Updated site to reflect new repository home
[treetop.git] / doc / contributing_and_planned_features.markdown
blobe3d4ab574a383ef91e20c5f0b93b00c50d655b5d
1 #Google Group
2 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.
3 treetop-dev@google-groups.com
5 #Contributing
6 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.
8 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.
11 ##Getting Started with the Code
12 Treetop compiler is interesting in that it is implemented in itself. Its functionality revolves around `metagrammar.treetop`, 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.
14 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 `metagrammar.treetop` 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.
16 ##Tests
17 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.
19 #What Needs to be Done
20 ##Small Stuff
21 * Improve the `tt` command line tool to allow `.treetop` extensions to be elided in its arguments.
22 * Generate and load temp files with `Treetop.load` rather than evaluating strings to improve stack trace readability.
23 * Allow `do/end` style blocks as well as curly brace blocks. This was originally omitted because I thought it would be confusing. It probably isn't.
25 ##Big Stuff
26 ####Transient Expressions
27 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.
29 ###Generate Rule Implementations in C
30 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.
32 ###Global Parsing State and Semantic Backtrack Triggering
33 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 _extrasyntactic_ 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.
35 Here is a sketch of the potential utility of such mechanisms. Consider the structure of YAML, which uses indentation to indicate block structure.
37     level_1:
38       level_2a:
39       level_2b:
40         level_3a:
41       level_2c:    
43 Imagine a grammar like the following:
45     rule yaml_element
46       name ':' block
47       /
48       name ':' value
49     end
50     
51     rule block
52       indent yaml_elements outdent
53     end
55     rule yaml_elements
56       yaml_element (samedent yaml_element)*
57     end
58     
59     rule samedent
60       newline spaces {
61         def after_success(parser_state)
62           spaces.length == parser_state.indent_level
63         end
64       }
65     end
66     
67     rule indent
68       newline spaces {
69         def after_success(parser_state)
70           if spaces.length == parser_state.indent_level + 2
71             parser_state.indent_level += 2
72             true
73           else
74             false # fail the parse on extrasyntactic grounds
75           end
76         end
77       
78         def undo_success(parser_state)
79           parser_state.indent_level -= 2
80         end
81       }
82     end
83     
84     rule outdent
85       newline spaces {
86         def after_success(parser_state)
87           if spaces.length == parser_state.indent_level - 2
88             parser_state.indent_level -= 2
89             true
90           else
91             false # fail the parse on extrasyntactic grounds
92           end
93         end
94       
95         def undo_success(parser_state)
96           parser_state.indent_level += 2
97         end
98       }
99     end
101 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.
103 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.