contribute.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <html><head><link href="./screen.css" rel="stylesheet" type="text/css" />
  2. <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
  3. </script>
  4. <script type="text/javascript">
  5. _uacct = "UA-3418876-1";
  6. urchinTracker();
  7. </script>
  8. </head><body><div id="top"><div id="main_navigation"><ul><li><a href="syntactic_recognition.html">Documentation</a></li><li>Contribute</li><li><a href="index.html">Home</a></li></ul></div></div><div id="middle"><div id="main_content"><h1>Google Group</h1>
  9. <p>I've created a <a href="http://groups.google.com/group/treetop-dev">Google Group</a> as a better place to organize discussion and development.
  10. treetop-dev@google-groups.com</p>
  11. <h1>Contributing</h1>
  12. <p>Visit <a href="http://github.com/nathansobo/treetop/tree/master">the Treetop repository page on GitHub</a> in your browser for more information about checking out the source code.</p>
  13. <p>I like to try Rubinius's policy regarding commit rights. If you submit one patch worth integrating, I'll give you commit rights. We'll see how this goes, but I think it's a good policy.</p>
  14. <h2>Getting Started with the Code</h2>
  15. <p>Treetop compiler is interesting in that it is implemented in itself. Its functionality revolves around <code>metagrammar.treetop</code>, which specifies the grammar for Treetop grammars. I took a hybrid approach with regard to definition of methods on syntax nodes in the metagrammar. Methods that are more syntactic in nature, like those that provide access to elements of the syntax tree, are often defined inline, directly in the grammar. More semantic methods are defined in custom node classes.</p>
  16. <p>Iterating on the metagrammar is tricky. The current testing strategy uses the last stable version of Treetop to parse the version under test. Then the version under test is used to parse and functionally test the various pieces of syntax it should recognize and translate to Ruby. As you change <code>metagrammar.treetop</code> and its associated node classes, note that the node classes you are changing are also used to support the previous stable version of the metagrammar, so must be kept backward compatible until such time as a new stable version can be produced to replace it.</p>
  17. <h2>Tests</h2>
  18. <p>Most of the compiler's tests are functional in nature. The grammar under test is used to parse and compile piece of sample code. Then I attempt to parse input with the compiled output and test its results.</p>
  19. <h1>What Needs to be Done</h1>
  20. <h2>Small Stuff</h2>
  21. <ul>
  22. <li>Improve the <code>tt</code> command line tool to allow <code>.treetop</code> extensions to be elided in its arguments.</li>
  23. <li>Generate and load temp files with <code>Treetop.load</code> rather than evaluating strings to improve stack trace readability.</li>
  24. <li>Allow <code>do/end</code> style blocks as well as curly brace blocks. This was originally omitted because I thought it would be confusing. It probably isn't.</li>
  25. </ul>
  26. <h2>Big Stuff</h2>
  27. <h4>Transient Expressions</h4>
  28. <p>Currently, every parsing expression instantiates a syntax node. This includes even very simple parsing expressions, like single characters. It is probably unnecessary for every single expression in the parse to correspond to its own syntax node, so much savings could be garnered from a transient declaration that instructs the parser only to attempt a match without instantiating nodes.</p>
  29. <h3>Generate Rule Implementations in C</h3>
  30. <p>Parsing expressions are currently compiled into simple Ruby source code that comprises the body of parsing rules, which are translated into Ruby methods. The generator could produce C instead of Ruby in the body of these method implementations.</p>
  31. <h3>Global Parsing State and Semantic Backtrack Triggering</h3>
  32. <p>Some programming language grammars are not entirely context-free, requiring that global state dictate the behavior of the parser in certain circumstances. Treetop does not currently expose explicit parser control to the grammar writer, and instead automatically constructs the syntax tree for them. A means of semantic parser control compatible with this approach would involve callback methods defined on parsing nodes. Each time a node is successfully parsed it will be given an opportunity to set global state and optionally trigger a parse failure on <em>extrasyntactic</em> grounds. Nodes will probably need to define an additional method that undoes their changes to global state when there is a parse failure and they are backtracked.</p>
  33. <p>Here is a sketch of the potential utility of such mechanisms. Consider the structure of YAML, which uses indentation to indicate block structure.</p>
  34. <pre><code>level_1:
  35. level_2a:
  36. level_2b:
  37. level_3a:
  38. level_2c:
  39. </code></pre>
  40. <p>Imagine a grammar like the following:</p>
  41. <pre><code>rule yaml_element
  42. name ':' block
  43. /
  44. name ':' value
  45. end
  46. rule block
  47. indent yaml_elements outdent
  48. end
  49. rule yaml_elements
  50. yaml_element (samedent yaml_element)*
  51. end
  52. rule samedent
  53. newline spaces {
  54. def after_success(parser_state)
  55. spaces.length == parser_state.indent_level
  56. end
  57. }
  58. end
  59. rule indent
  60. newline spaces {
  61. def after_success(parser_state)
  62. if spaces.length == parser_state.indent_level + 2
  63. parser_state.indent_level += 2
  64. true
  65. else
  66. false # fail the parse on extrasyntactic grounds
  67. end
  68. end
  69. def undo_success(parser_state)
  70. parser_state.indent_level -= 2
  71. end
  72. }
  73. end
  74. rule outdent
  75. newline spaces {
  76. def after_success(parser_state)
  77. if spaces.length == parser_state.indent_level - 2
  78. parser_state.indent_level -= 2
  79. true
  80. else
  81. false # fail the parse on extrasyntactic grounds
  82. end
  83. end
  84. def undo_success(parser_state)
  85. parser_state.indent_level += 2
  86. end
  87. }
  88. end
  89. </code></pre>
  90. <p>In this case a block will be detected only if a change in indentation warrants it. Note that this change in the state of indentation must be undone if a subsequent failure causes this node not to ultimately be incorporated into a successful result.</p>
  91. <p>I am by no means sure that the above sketch is free of problems, or even that this overall strategy is sound, but it seems like a promising path.</p></div></div><div id="bottom"></div></body></html>