Guest User

Untitled

a guest
Jul 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. // Invocation flow
  2.  
  3. // Kod opens a new file and initially sends the entire document to node for
  4. // parsing. UID is an opaque symbol identifying the translation unit ("file") in
  5. // a global space.
  6. // Here, source is "var x = 4 + 5;"
  7. [kod -> node] parse(UID, language, name, source)
  8. // node finds an appropriate parser for language and parses the source
  9. var parser = parserForLanguage(language)
  10. var ast = parser.parse(source)
  11. index[UID] = {ast:ast, name:name}
  12.  
  13. // then node sends an "ast updated for UID" message to kod
  14. [node -> kod] updateAST(UID, AST, extent=[0,14])
  15.  
  16. // now, the source is updated due to an edit -- source is now:
  17. // "var x = 4 + 5;\nfoo();"
  18. // so kod sends a request for incremental parsing. "source" passed to node is:
  19. // "\nfoo();" which is all text after the previous ast node and up until the
  20. // next affected ast node.
  21. [kod -> node] parse(UID, language, name, source="\nfoo();",
  22. modifiedRange=[14,0],
  23. changeLengthDelta=7)
  24. // kod finds the appropriate parser and loads the AST
  25. var parser = parserForLanguage(language)
  26. var ast = index[UID].ast
  27. // the parser deduces the "farthest" (most minimal) branch which need to be
  28. // re-parsed as an effect of the edit:
  29. var affectedNode = parser.affectedNode(ast, modifiedRange,
  30. changeLengthDelta)
  31. // parses and patches the ast
  32. parser.parseAndPatch(source, ast, modifiedRange)
  33. // finally node sends an "ast updated for UID" message to kod, passing the new
  34. // ast and telling kod to what extent the source was affected
  35. [node -> kod] updateAST(UID, AST, affectedNode.extent)
  36.  
  37. // now, an edit is made which modifies an ast node/branch so source become:
  38. // "var x = 4 + 5;\nfoo(x);" (added: passing "x" as an argument to "foo")
  39. // kod sends a "parse" request:
  40. [kod -> node] parse(UID, language, name, source="x",
  41. modifiedRange=[20,0],
  42. changeLengthDelta=1)
  43. // kod finds the appropriate parser and loads the AST
  44. var parser = parserForLanguage(language)
  45. var ast = index[UID].ast
  46. // the parser deduces the "farthest" (most minimal) branch which need to be
  47. // re-parsed as an effect of the edit:
  48. var affectedNode = parser.affectedNode(ast, modifiedRange,
  49. changeLengthDelta)
  50. // the source is expanded to include the extent of affectedNode
  51. // TODO: "text" is the complete text of the translation unit, but need an API
  52. var newExtent = affectedNode.extent
  53. newExtent.length += changeLengthDelta
  54. source = text.substr(newExtent.location, newExtent.length)
  55. // parses and patches the ast
  56. parser.parseAndPatch(source, ast, affectedNode)
  57. // finally we send a "ast updated for UID" message to kod, passing the new
  58. // ast and telling kod to what extent the source was affected
  59. [node -> kod] updateAST(UID, AST, affectedNode.extent)
Add Comment
Please, Sign In to add comment