annotate src/editor.tp @ 245:3590ecca6bc9

Fix handling of unescaped tabs in string literals in new parser
author Mike Pavone <pavone@retrodev.com>
date Mon, 06 Jan 2014 01:03:18 -0800
parents 15aac5334b64
children
rev   line source
pavone@11 1 #{
pavone@11 2 //mquery functions
pavone@11 3 q <- foreign: :query {}
pavone@11 4 qall <- foreign: :query {}
pavone@11 5 each <- foreign: :iterable fun {}
pavone@11 6 addClass <- foreign: :node className {}
pavone@11 7 removeClass <- foreign: :node className {}
pavone@110 8 hasClass <- foreign: :node className {}
pavone@12 9 get <- foreign: :url onSuccess onFail onOther {}
pavone@127 10 newEl <- foreign: :tagname props {}
pavone@127 11
pavone@127 12 //editor.js functions
pavone@110 13 getEl <- foreign: :from idx {}
pavone@110 14 setEl <- foreign: :to idx val {}
pavone@127 15 goFullScreen <- foreign: {}
pavone@141 16 create_symbol <- foreign: :name {}
pavone@11 17
pavone@14 18 //TP Parser
pavone@14 19 parser <- foreign: #{
pavone@14 20 parse <- foreign: :str {}
pavone@14 21 }
pavone@25 22 isLambda <- foreign: :astnode {}
pavone@14 23
pavone@11 24 //js builtins
pavone@11 25 console <- foreign: #{
pavone@23 26 log <- foreign: :val {}
pavone@11 27 }
pavone@25 28 window <- foreign: #{}
pavone@23 29 Object <- foreign: #{
pavone@23 30 keys <- foreign: :object {}
pavone@23 31 }
pavone@11 32
pavone@108 33 filter <- :arr pred {
pavone@108 34 output <- arr slice: 0 0
pavone@108 35 each: arr :idx el {
pavone@108 36 if: (pred: el) {
pavone@108 37 output push: el
pavone@108 38 } else: {}
pavone@108 39 }
pavone@108 40 output
pavone@108 41 }
pavone@108 42
pavone@25 43 //editor code
pavone@113 44 selection <- #{
pavone@113 45 valid? <- false
pavone@113 46 }
pavone@113 47
pavone@117 48 setSelection:withInNode <- :astnode :innode {
pavone@117 49 fakeEvent <- #{
pavone@117 50 stopPropagation <- :Blah {
pavone@117 51 }
pavone@117 52 }
pavone@131 53
pavone@117 54 selection <- #{
pavone@117 55 valid? <- true
pavone@117 56 in <- {
pavone@117 57 (innode domNode) onclick: fakeEvent
pavone@117 58 }
pavone@117 59 out <- {
pavone@117 60 ((astnode up) domNode) onclick: fakeEvent
pavone@117 61 }
pavone@131 62 next <- {
pavone@131 63 (((astnode up) getNext: astnode) domNode) onclick: fakeEvent
pavone@131 64 }
pavone@131 65
pavone@131 66 previous <- {
pavone@131 67 (((astnode up) getPrev: astnode) domNode) onclick: fakeEvent
pavone@131 68 }
pavone@117 69 }
pavone@117 70 }
pavone@117 71
pavone@118 72 setSelection <- :astnode {
pavone@118 73 fakeEvent <- #{
pavone@118 74 stopPropagation <- :Blah {
pavone@118 75 }
pavone@118 76 }
pavone@118 77 selection <- #{
pavone@118 78 valid? <- true
pavone@118 79 in <- {
pavone@118 80 }
pavone@118 81 out <- {
pavone@118 82 fakeEvent <- #{
pavone@118 83 stopPropagation <- :Blah {
pavone@118 84 }
pavone@118 85 }
pavone@118 86 ((astnode up) domNode) onclick: fakeEvent
pavone@118 87 }
pavone@131 88 next <- {
pavone@131 89 console log: "selection next"
pavone@131 90 (((astnode up) getNext: astnode) domNode) onclick: fakeEvent
pavone@131 91 }
pavone@131 92 previous <- {
pavone@131 93 (((astnode up) getPrev: astnode) domNode) onclick: fakeEvent
pavone@131 94 }
pavone@118 95 }
pavone@118 96 }
pavone@118 97
pavone@17 98 editFile <- :path {
pavone@17 99 get: path :request {
pavone@17 100 addClass: (q: "body") "editorMode"
pavone@21 101 src <- request responseText
pavone@17 102 ast <- parser parse: src
pavone@23 103 ast populateSymbols: (foreign: null)
pavone@17 104 ast toHTML: (q: "#src")
pavone@17 105 }
pavone@17 106 }
pavone@17 107
pavone@25 108 selectNode <- :node {
pavone@25 109 each: (qall: ".selected") :idx el {
pavone@25 110 removeClass: el "selected"
pavone@25 111 }
pavone@25 112 addClass: node "selected"
pavone@25 113 }
pavone@25 114
pavone@25 115 selectQuery <- :selector {
pavone@25 116 selectQuery: selector in: (foreign: undefined)
pavone@25 117 }
pavone@25 118
pavone@25 119 selectQuery:in <- :selector :context {
pavone@25 120 each: (qall: ".selected") :idx el {
pavone@25 121 removeClass: el "selected"
pavone@25 122 }
pavone@25 123 each: (qall: selector context) :idx el {
pavone@25 124 addClass: el "selected"
pavone@25 125 }
pavone@25 126 }
pavone@25 127
pavone@25 128 selectParent <- :node {
pavone@25 129 each: (qall: ".selectParent") :idx el {
pavone@109 130 removeClass: el "selectParent"
pavone@25 131 }
pavone@113 132 addClass: node "selectParent"
pavone@25 133 }
pavone@25 134
pavone@25 135 popInscope:onClick <- :syms :handler {
pavone@23 136 inscope <- q: "#inscope"
pavone@23 137 inscope innerHTML!: ""
pavone@24 138 each: syms :idx key {
pavone@23 139 inscope appendChild: (newEl: "li" #{
pavone@23 140 textContent <- key
pavone@107 141 onclick <- :Event { handler: key }
pavone@23 142 })
pavone@23 143 }
pavone@23 144 }
pavone@23 145
pavone@119 146 scalarClick <- :domnode astnode event {
pavone@119 147 selectNode: domnode
pavone@119 148 setSelection: astnode
pavone@119 149 event stopPropagation: (foreign: undefined)
pavone@119 150 //TODO: set focus
pavone@119 151 }
pavone@119 152
pavone@29 153 symbolClick <- :domnode astnode event {
pavone@25 154 selectNode: domnode
pavone@105 155 popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
pavone@25 156 domnode textContent!: key
pavone@25 157 astnode name!: key
pavone@25 158 }
pavone@118 159 setSelection: astnode
pavone@118 160 event stopPropagation: (foreign: undefined)
pavone@118 161 }
pavone@118 162
pavone@118 163 assignClick <- :domnode astnode event {
pavone@118 164 selectParent: domnode
pavone@118 165 selectQuery: ".selectParent > .varname" in: domnode
pavone@118 166 popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
pavone@118 167 (domnode firstChild) textContent!: key
pavone@118 168 (astnode symbol) name!: key
pavone@118 169 }
pavone@118 170 setSelection: astnode withInNode: (astnode expression)
pavone@107 171 event stopPropagation: (foreign: undefined)
pavone@25 172 }
pavone@25 173
pavone@116 174 opClick <- :domnode astnode event {
pavone@116 175 selectParent: domnode
pavone@116 176 selectQuery: ".selectParent > .opname" in: domnode
pavone@116 177 showOps
pavone@117 178 setSelection: astnode withInNode: (astnode left)
pavone@116 179 event stopPropagation: (foreign: undefined)
pavone@116 180 }
pavone@116 181
pavone@29 182 funClick <- :domnode astnode event {
pavone@25 183 selectParent: domnode
pavone@113 184 selectQuery: ".selectParent > .funpart" in: domnode
pavone@25 185 symtable <- astnode symbols
pavone@108 186 syms <- filter: (symtable allSymbols: (foreign: undefined)) :sym {
pavone@25 187 isLambda: ((symtable find: sym) def)
pavone@25 188 }
pavone@117 189 inner <- if: (astnode receiver) != (foreign: null) {
pavone@117 190 astnode receiver
pavone@117 191 } else: {
pavone@117 192 (astnode args) getEl: 0
pavone@113 193 }
pavone@117 194 setSelection: astnode withInNode: inner
pavone@110 195 popInscope: syms onClick: :key {
pavone@110 196 astnode name!: key
pavone@110 197 parts <- key split: ":"
pavone@110 198 nodes <- []
pavone@113 199 each: (domnode children) :idx val{
pavone@110 200 nodes push: val
pavone@110 201 }
pavone@110 202 partIdx <- 0
pavone@110 203 nodeIdx <- 0
pavone@110 204 lastWasNamePart <- true
pavone@110 205 while: { partIdx < (parts length) || nodeIdx < (nodes length) } do: {
pavone@110 206 if: nodeIdx < (nodes length) {
pavone@110 207 node <-getEl: nodes nodeIdx
pavone@110 208 nodeIdx <- nodeIdx + 1
pavone@110 209 if: (hasClass: node "funpart") {
pavone@110 210 if: partIdx < (parts length) {
pavone@110 211 postfix <- if: partIdx = 0 && nodeIdx = 2 && (parts length) = 1 && (nodes length) = 2 { "" } else: { ":" }
pavone@110 212 t <- (getEl: parts partIdx)
pavone@110 213 node textContent!: (getEl: parts partIdx) . postfix
pavone@110 214 partIdx <- partIdx + 1
pavone@110 215 } else: {
pavone@113 216 domnode removeChild: node
pavone@110 217 }
pavone@110 218 lastWasNamePart <- true
pavone@110 219 } else: {
pavone@110 220 if: (not: lastWasNamePart) && partIdx < (parts length) && nodeIdx > 0 {
pavone@113 221 domnode insertBefore: (newEl: "span" #{
pavone@110 222 className <- "funpart selected"
pavone@110 223 textContent <- (getEl: parts partIdx) . ":"
pavone@110 224 }) node
pavone@110 225 partIdx <- partIdx + 1
pavone@110 226 }
pavone@110 227 lastWasNamePart <- false
pavone@110 228 }
pavone@110 229 } else: {
pavone@110 230 console log: "part: " . (getEl: parts partIdx)
pavone@113 231 domnode appendChild: (newEl: "span" #{
pavone@110 232 className <- "funpart selected"
pavone@110 233 textContent <- (getEl: parts partIdx) . ":"
pavone@110 234 })
pavone@110 235 partIdx <- partIdx + 1
pavone@110 236 }
pavone@110 237 }
pavone@110 238 }
pavone@107 239 event stopPropagation: (foreign: undefined)
pavone@25 240 }
pavone@25 241
pavone@141 242 replaceNode:with <- :astnode domnode :newnode {
pavone@141 243
pavone@141 244 }
pavone@141 245
pavone@29 246 lambdaClick <- :domnode astnode event {
pavone@119 247 selectNode: domnode
pavone@119 248 popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
pavone@141 249 replaceNode: astnode domnode with: (create_symbol: key)
pavone@119 250 }
pavone@119 251 inner <- if: ((astnode args) length) > 0 {
pavone@119 252 (astnode args) getEl: 0
pavone@119 253 } else: {
pavone@119 254 (astnode expressions) getEl: 0
pavone@119 255 }
pavone@119 256 setSelection: astnode withInNode: inner
pavone@119 257 event stopPropagation: (foreign: undefined)
pavone@28 258 }
pavone@28 259
pavone@120 260 objectClick <- :domnode astnode event {
pavone@120 261 selectNode: domnode
pavone@120 262 popInscope: ((astnode symbols) allSymbols: (foreign: undefined)) onClick: :key {
pavone@120 263 console log: "fooobar!"
pavone@120 264 }
pavone@124 265 setSelection: astnode withInNode: ((astnode messages) getEl: 0)
pavone@120 266 event stopPropagation: (foreign: undefined)
pavone@120 267 }
pavone@120 268
pavone@116 269 visible <- "showops"
pavone@116 270
pavone@116 271 showOps <- {
pavone@116 272 each: (qall: ".controls") :idx el {
pavone@116 273 removeClass: el visible
pavone@116 274 addClass: el "showops"
pavone@116 275 }
pavone@116 276 visible <- "showops"
pavone@116 277 }
pavone@116 278
pavone@116 279 showLit <- {
pavone@116 280 each: (qall: ".controls") :idx el {
pavone@116 281 removeClass: el visible
pavone@116 282 addClass: el "showlit"
pavone@116 283 }
pavone@116 284 visible <- "showlit"
pavone@116 285 }
pavone@116 286
pavone@11 287 main <- {
pavone@30 288 get: "/src/" :data {
pavone@30 289 fakeEl <- newEl: "div" #{
pavone@30 290 innerHTML <- data response
pavone@30 291 }
pavone@30 292 each: (qall: "a" fakeEl) :idx el {
pavone@30 293 if: ((el textContent) = "../") {} else: {
pavone@30 294 nel <- newEl: "a" #{
pavone@30 295 href <- "/edit/src/" + (el textContent)
pavone@30 296 textContent <- el textContent
pavone@30 297 }
pavone@30 298 nel onclick!: :event {
pavone@30 299 link <- foreign: this
pavone@30 300 path <- link href
pavone@30 301 path <- path substr: (path indexOf: "/edit/") + 5
pavone@30 302 editFile: path
pavone@30 303 foreign: false
pavone@30 304 }
pavone@30 305 li <- newEl: "li"
pavone@30 306 li appendChild: nel
pavone@30 307 (q: "#browser ul") appendChild: li
pavone@30 308 }
pavone@12 309 }
pavone@12 310 }
pavone@124 311
pavone@12 312 //bind handlers for editor buttons
pavone@12 313 each: (qall: ".controls li") :idx el {
pavone@21 314 el onclick!: :event {
pavone@11 315 srcel <- (q: "#src")
pavone@21 316 srcel textContent!: (srcel textContent) + (el textContent)
pavone@11 317 }
pavone@11 318 }
pavone@21 319 (q: "#ops_button") onclick!: :event {
pavone@116 320 showOps
pavone@111 321 }
pavone@111 322 (q: "#lit_button") onclick!: :event {
pavone@116 323 showLit
pavone@11 324 }
pavone@124 325
pavone@113 326 (q: "#in") onclick!: :event {
pavone@113 327 console log: "inwards"
pavone@113 328 if: (selection valid?) {
pavone@113 329 selection in
pavone@113 330 }
pavone@113 331 }
pavone@124 332
pavone@113 333 (q: "#out") onclick!: :event {
pavone@113 334 console log: "outwards"
pavone@113 335 if: (selection valid?) {
pavone@113 336 selection out
pavone@113 337 }
pavone@113 338 }
pavone@124 339
pavone@131 340 (q: "#next") onclick!: :event {
pavone@131 341 if: (selection valid?) {
pavone@131 342 selection next
pavone@131 343 }
pavone@131 344 }
pavone@131 345
pavone@131 346 (q: "#prev") onclick!: :event {
pavone@131 347 if: (selection valid?) {
pavone@131 348 selection previous
pavone@131 349 }
pavone@131 350 }
pavone@131 351
pavone@21 352 path <- (window location) pathname
pavone@17 353 if: (path indexOf: "/edit/") = 0 {
pavone@17 354 editFile: (path substr: 5)
pavone@113 355 }
pavone@127 356 (q: "#fullscreen") onclick!: :event {
pavone@127 357 goFullScreen:
pavone@127 358 }
pavone@11 359 }
pavone@11 360
pavone@11 361 }