treetop 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/Rakefile +1 -1
  2. data/doc/contributing_and_planned_features.markdown +3 -11
  3. data/doc/index.markdown +65 -4
  4. data/doc/semantic_interpretation.markdown +3 -1
  5. data/doc/site.rb +79 -10
  6. data/doc/site/contribute.html +118 -0
  7. data/doc/{images/middle_backgound.png → site/images/bottom_background.png} +0 -0
  8. data/doc/{images → site/images}/middle_background.png +0 -0
  9. data/doc/{images → site/images}/paren_language_output.png +0 -0
  10. data/doc/site/images/pivotal.gif +0 -0
  11. data/doc/site/images/top_background.png +0 -0
  12. data/doc/site/index.html +102 -0
  13. data/doc/site/pitfalls_and_advanced_techniques.html +68 -0
  14. data/doc/site/screen.css +129 -0
  15. data/doc/site/semantic_interpretation.html +214 -0
  16. data/doc/site/syntactic_recognition.html +142 -0
  17. data/doc/site/using_in_ruby.html +34 -0
  18. data/doc/sitegen.rb +60 -0
  19. data/doc/syntactic_recognition.markdown +11 -14
  20. data/doc/using_in_ruby.markdown +7 -3
  21. data/lib/treetop/compiler/metagrammar.rb +2 -2
  22. data/lib/treetop/compiler/metagrammar.treetop +3 -3
  23. data/lib/treetop/compiler/node_classes.rb +1 -0
  24. data/lib/treetop/compiler/node_classes/character_class.rb +5 -1
  25. data/lib/treetop/compiler/node_classes/predicate.rb +1 -1
  26. data/lib/treetop/compiler/node_classes/transient_prefix.rb +9 -0
  27. data/lib/treetop/runtime.rb +2 -1
  28. data/lib/treetop/runtime/interval_skip_list.rb +4 -0
  29. data/lib/treetop/runtime/interval_skip_list/head_node.rb +15 -0
  30. data/lib/treetop/runtime/interval_skip_list/interval_skip_list.rb +200 -0
  31. data/lib/treetop/runtime/interval_skip_list/node.rb +164 -0
  32. data/lib/treetop/runtime/syntax_node.rb +40 -40
  33. metadata +23 -10
  34. data/doc/images/bottom_background.png +0 -0
  35. data/doc/images/top_background.png +0 -0
  36. data/doc/screen.css +0 -52
  37. data/doc/site.html +0 -34
@@ -0,0 +1,68 @@
1
+ <html><head><link rel="stylesheet" href="./screen.css" type="text/css"></link>
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>Documentation</li><li><a href="contribute.html">Contribute</a></li><li><a href="index.html">Home</a></li></ul></div></div><div id="middle"><div id="content"><div id="secondary_navigation"><ul><li><a href="syntactic_recognition.html">Syntax</a></li><li><a href="semantic_interpretation.html">Semantics</a></li><li><a href="using_in_ruby.html">Using In Ruby</a></li><li>Advanced Techniques</li></ul></div><div id="documentation_content"><h1>Pitfalls</h1>
9
+
10
+ <h2>Left Recursion</h2>
11
+
12
+ <p>An weakness shared by all recursive descent parsers is the inability to parse left-recursive rules. Consider the following rule:</p>
13
+
14
+ <pre><code>rule left_recursive
15
+ left_recursive 'a' / 'a'
16
+ end
17
+ </code></pre>
18
+
19
+ <p>Logically it should match a list of 'a' characters. But it never consumes anything, because attempting to recognize <code>left_recursive</code> begins by attempting to recognize <code>left_recursive</code>, and so goes an infinite recursion. There's always a way to eliminate these types of structures from your grammar. There's a mechanistic transformation called <em>left factorization</em> that can eliminate it, but it isn't always pretty, especially in combination with automatically constructed syntax trees. So far, I have found more thoughtful ways around the problem. For instance, in the interpreter example I interpret inherently left-recursive function application right recursively in syntax, then correct the directionality in my semantic interpretation. You may have to be clever.</p>
20
+
21
+ <h1>Advanced Techniques</h1>
22
+
23
+ <p>Here are a few interesting problems I've encountered. I figure sharing them may give you insight into how these types of issues are addressed with the tools of parsing expressions.</p>
24
+
25
+ <h2>Matching a String</h2>
26
+
27
+ <pre><code>rule string
28
+ '"' (!'"' . / '\"')* '"'
29
+ end
30
+ </code></pre>
31
+
32
+ <p>This expression says: Match a quote, then zero or more of any character but a quote or an escaped quote followed by a quote. Lookahead assertions are essential for these types of problems.</p>
33
+
34
+ <h2>Matching Nested Structures With Non-Unique Delimeters</h2>
35
+
36
+ <p>Say I want to parse a diabolical wiki syntax in which the following interpretations apply.</p>
37
+
38
+ <pre><code>** *hello* ** --&gt; &lt;strong&gt;&lt;em&gt;hello&lt;/em&gt;&lt;/strong&gt;
39
+ * **hello** * --&gt; &lt;em&gt;&lt;strong&gt;hello&lt;/strong&gt;&lt;/em&gt;
40
+
41
+ rule strong
42
+ '**' (em / !'*' . / '*')+ '**'
43
+ end
44
+
45
+ rule em
46
+ '**' (strong / !'*' . / '*')+ '**'
47
+ end
48
+ </code></pre>
49
+
50
+ <p>Emphasized text is allowed within strong text by virtue of <code>em</code> being the first alternative. Since <code>em</code> will only successfully parse if a matching <code>*</code> is found, it is permitted, but other than that, no <code>*</code> characters are allowed unless they are escaped.</p>
51
+
52
+ <h2>Matching a Keyword But Not Words Prefixed Therewith</h2>
53
+
54
+ <p>Say I want to consider a given string a characters only when it occurs in isolation. Lets use the <code>end</code> keyword as an example. We don't want the prefix of <code>'enders_game'</code> to be considered a keyword. A naiive implementation might be the following.</p>
55
+
56
+ <pre><code>rule end_keyword
57
+ 'end' &amp;space
58
+ end
59
+ </code></pre>
60
+
61
+ <p>This says that <code>'end'</code> must be followed by a space, but this space is not consumed as part of the matching of <code>keyword</code>. This works in most cases, but is actually incorrect. What if <code>end</code> occurs at the end of the buffer? In that case, it occurs in isolation but will not match the above expression. What we really mean is that <code>'end'</code> cannot be followed by a <em>non-space</em> character.</p>
62
+
63
+ <pre><code>rule end_keyword
64
+ 'end' !(!' ' .)
65
+ end
66
+ </code></pre>
67
+
68
+ <p>In general, when the syntax gets tough, it helps to focus on what you really mean. A keyword is a character not followed by another character that isn't a space.</p></div></div></div><div id="bottom"></div></body></html>
@@ -0,0 +1,129 @@
1
+ body {
2
+ margin: 0;
3
+ padding: 0;
4
+ background: #666666;
5
+ font-family: "Lucida Grande", Geneva, Arial, Verdana, sans-serif;
6
+ color: #333333;
7
+ }
8
+
9
+ div {
10
+ margin: 0;
11
+ background-position: center;
12
+ background-repeat: none;
13
+ }
14
+
15
+ h1 {
16
+ font-size: 125%;
17
+ margin-top: 1.5em;
18
+ margin-bottom: .5em;
19
+ }
20
+
21
+ h2 {
22
+ font-size: 115%;
23
+ margin-top: 3em;
24
+ margin-bottom: .5em;
25
+ }
26
+
27
+ h3 {
28
+ font-size: 105%;
29
+ margin-top: 1.5em;
30
+ margin-bottom: .5em;
31
+ }
32
+
33
+ a {
34
+ color: #ff8429;
35
+ }
36
+
37
+
38
+ div#top {
39
+ background-image: url( "images/top_background.png" );
40
+ height: 200px;
41
+ width: 100%;
42
+ }
43
+
44
+ div#middle {
45
+ padding-top: 10px;
46
+ background-image: url( "images/middle_background.png" );
47
+ background-repeat: repeat-y;
48
+ }
49
+
50
+ div#bottom {
51
+ background-image: url( "images/bottom_background.png" );
52
+ height: 13px;
53
+ margin-bottom: 30px;
54
+ }
55
+
56
+ div#main_navigation {
57
+ width: 300px;
58
+ margin: 0px auto 0 auto;
59
+ padding-top: 43px;
60
+ padding-right: 10px;
61
+ position: relative;
62
+ right: 500px;
63
+ text-align: right;
64
+ line-height: 130%;
65
+ font-size: 90%;
66
+ }
67
+
68
+ div#main_navigation ul {
69
+ list-style-type: none;
70
+ padding: 0;
71
+ }
72
+
73
+ div#main_navigation a, div#main_navigation a:visited {
74
+ color: white;
75
+ text-decoration: none;
76
+ }
77
+
78
+ div#main_navigation a:hover {
79
+ text-decoration: underline;
80
+ }
81
+
82
+ div#secondary_navigation {
83
+ position: relative;
84
+ font-size: 90%;
85
+ margin: 0 auto 0 auto;
86
+ padding: 0px;
87
+ text-align: center;
88
+ position: relative;
89
+ top: -10px;
90
+ }
91
+
92
+ div#secondary_navigation ul {
93
+ list-style-type: none;
94
+ padding: 0;
95
+ }
96
+
97
+ div#secondary_navigation li {
98
+ display: inline;
99
+ margin-left: 10px;
100
+ margin-right: 10px;
101
+ }
102
+
103
+ div#content {
104
+ width: 545px;
105
+ margin: 0 auto 0 auto;
106
+ padding: 0 60px 25px 60px;
107
+ }
108
+
109
+ pre {
110
+ background: #333333;
111
+ color: white;
112
+ padding: 15px;
113
+ border: 1px solid #666666;
114
+ }
115
+
116
+ p {
117
+ line-height: 150%;
118
+ }
119
+
120
+ p.intro_text {
121
+ color: #C45900;
122
+ font-size: 115%;
123
+ }
124
+
125
+ img#pivotal_logo {
126
+ border: none;
127
+ margin-left: auto;
128
+ margin-right: auto;
129
+ }
@@ -0,0 +1,214 @@
1
+ <html><head><link rel="stylesheet" href="./screen.css" type="text/css"></link>
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>Documentation</li><li><a href="contribute.html">Contribute</a></li><li><a href="index.html">Home</a></li></ul></div></div><div id="middle"><div id="content"><div id="secondary_navigation"><ul><li><a href="syntactic_recognition.html">Syntax</a></li><li>Semantics</li><li><a href="using_in_ruby.html">Using In Ruby</a></li><li><a href="pitfalls_and_advanced_techniques.html">Advanced Techniques</a></li></ul></div><div id="documentation_content"><h1>Semantic Interpretation</h1>
9
+
10
+ <p>Lets use the below grammar as an example. It describes parentheses wrapping a single character to an arbitrary depth.</p>
11
+
12
+ <pre><code>grammar ParenLanguage
13
+ rule parenthesized_letter
14
+ '(' parenthesized_letter ')'
15
+ /
16
+ [a-z]
17
+ end
18
+ end
19
+ </code></pre>
20
+
21
+ <p>Matches:</p>
22
+
23
+ <ul>
24
+ <li><code>'a'</code></li>
25
+ <li><code>'(a)'</code></li>
26
+ <li><code>'((a))'</code></li>
27
+ <li>etc.</li>
28
+ </ul>
29
+
30
+ <p>Output from a parser for this grammar looks like this:</p>
31
+
32
+ <p><img src="./images/paren_language_output.png" alt="Tree Returned By ParenLanguageParser"/></p>
33
+
34
+ <p>This is a parse tree whose nodes are instances of <code>Treetop::Runtime::SyntaxNode</code>. What if we could define methods on these node objects? We would then have an object-oriented program whose structure corresponded to the structure of our language. Treetop provides two techniques for doing just this.</p>
35
+
36
+ <h2>Associating Methods with Node-Instantiating Expressions</h2>
37
+
38
+ <p>Sequences and all types of terminals are node-instantiating expressions. When they match, they create instances of <code>Treetop::Runtime::SyntaxNode</code>. Methods can be added to these nodes in the following ways:</p>
39
+
40
+ <h3>Inline Method Definition</h3>
41
+
42
+ <p>Methods can be added to the nodes instantiated by the successful match of an expression</p>
43
+
44
+ <pre><code>grammar ParenLanguage
45
+ rule parenthesized_letter
46
+ '(' parenthesized_letter ')' {
47
+ def depth
48
+ parenthesized_letter.depth + 1
49
+ end
50
+ }
51
+ /
52
+ [a-z] {
53
+ def depth
54
+ 0
55
+ end
56
+ }
57
+ end
58
+ end
59
+ </code></pre>
60
+
61
+ <p>Note that each alternative expression is followed by a block containing a method definition. A <code>depth</code> method is defined on both expressions. The recursive <code>depth</code> method defined in the block following the first expression determines the depth of the nested parentheses and adds one two it. The base case is implemented in the block following the second expression; a single character has a depth of 0.</p>
62
+
63
+ <h3>Custom <code>SyntaxNode</code> Subclass Declarations</h3>
64
+
65
+ <p>You can instruct the parser to instantiate a custom subclass of Treetop::Runtime::SyntaxNode for an expression by following it by the name of that class enclosed in angle brackets (<code>&lt;&gt;</code>). The above inline method definitions could have been moved out into a single class like so.</p>
66
+
67
+ <pre><code># in .treetop file
68
+ grammar ParenLanguage
69
+ rule parenthesized_letter
70
+ '(' parenthesized_letter ')' &lt;ParenNode&gt;
71
+ /
72
+ [a-z] &lt;ParenNode&gt;
73
+ end
74
+ end
75
+
76
+ # in separate .rb file
77
+ class ParenNode &lt; Treetop::Runtime::SyntaxNode
78
+ def depth
79
+ if nonterminal?
80
+ parenthesized_letter.depth + 1
81
+ else
82
+ 0
83
+ end
84
+ end
85
+ end
86
+ </code></pre>
87
+
88
+ <h2>Automatic Extension of Results</h2>
89
+
90
+ <p>Nonterminal and ordered choice expressions do not instantiate new nodes, but rather pass through nodes that are instantiated by other expressions. They can extend nodes they propagate with anonymous or declared modules, using similar constructs used with expressions that instantiate their own syntax nodes.</p>
91
+
92
+ <h3>Extending a Propagated Node with an Anonymous Module</h3>
93
+
94
+ <pre><code>rule parenthesized_letter
95
+ ('(' parenthesized_letter ')' / [a-z]) {
96
+ def depth
97
+ if nonterminal?
98
+ parenthesized_letter.depth + 1
99
+ else
100
+ 0
101
+ end
102
+ end
103
+ }
104
+ end
105
+ </code></pre>
106
+
107
+ <p>The parenthesized choice above can result in a node matching either of the two choices. Than node will be extended with methods defined in the subsequent block. Note that a choice must always be parenthesized to be associated with a following block.</p>
108
+
109
+ <h3>Extending A Propagated Node with a Declared Module</h3>
110
+
111
+ <pre><code># in .treetop file
112
+ rule parenthesized_letter
113
+ ('(' parenthesized_letter ')' / [a-z]) &lt;ParenNode&gt;
114
+ end
115
+
116
+ # in separate .rb file
117
+ module ParenNode
118
+ def depth
119
+ if nonterminal?
120
+ parenthesized_letter.depth + 1
121
+ else
122
+ 0
123
+ end
124
+ end
125
+ end
126
+ </code></pre>
127
+
128
+ <p>Here the result is extended with the <code>ParenNode</code> module. Note the previous example for node-instantiating expressions, the constant in the declaration must be a module because the result is extended with it.</p>
129
+
130
+ <h2>Automatically-Defined Element Accessor Methods</h2>
131
+
132
+ <h3>Default Accessors</h3>
133
+
134
+ <p>Nodes instantiated upon the matching of sequences have methods automatically defined for any nonterminals in the sequence.</p>
135
+
136
+ <pre><code>rule abc
137
+ a b c {
138
+ def to_s
139
+ a.to_s + b.to_s + c.to_s
140
+ end
141
+ }
142
+ end
143
+ </code></pre>
144
+
145
+ <p>In the above code, the <code>to_s</code> method calls automatically-defined element accessors for the nodes returned by parsing nonterminals <code>a</code>, <code>b</code>, and <code>c</code>. </p>
146
+
147
+ <h3>Labels</h3>
148
+
149
+ <p>Subexpressions can be given an explicit label to have an element accessor method defined for them. This is useful in cases of ambiguity between two references to the same nonterminal or when you need to access an unnamed subexpression.</p>
150
+
151
+ <pre><code>rule labels
152
+ first_letter:[a-z] rest_letters:(', ' letter:[a-z])* {
153
+ def letters
154
+ [first_letter] + rest_letters.map do |comma_and_letter|
155
+ comma_and_letter.letter
156
+ end
157
+ end
158
+ }
159
+ end
160
+ </code></pre>
161
+
162
+ <p>The above grammar uses label-derived accessors to determine the letters in a comma-delimited list of letters. The labeled expressions <em>could</em> have been extracted to their own rules, but if they aren't used elsewhere, labels still enable them to be referenced by a name within the expression's methods.</p>
163
+
164
+ <h3>Overriding Element Accessors</h3>
165
+
166
+ <p>The module containing automatically defined element accessor methods is an ancestor of the module in which you define your own methods, meaning you can override them with access to the <code>super</code> keyword. Here's an example of how this fact can improve the readability of the example above.</p>
167
+
168
+ <pre><code>rule labels
169
+ first_letter:[a-z] rest_letters:(', ' letter:[a-z])* {
170
+ def letters
171
+ [first_letter] + rest_letters
172
+ end
173
+
174
+ def rest_letters
175
+ super.map { |comma_and_letter| comma_and_letter.letter }
176
+ end
177
+ }
178
+ end
179
+ </code></pre>
180
+
181
+ <h2>Methods Available on <code>Treetop::Runtime::SyntaxNode</code></h2>
182
+
183
+ <table>
184
+ <tr>
185
+ <td>
186
+ <code>terminal?</code>
187
+ </td>
188
+ <td>
189
+ Was this node produced by the matching of a terminal symbol?
190
+ </td>
191
+ </tr>
192
+ <tr>
193
+ <td>
194
+ <code>nonterminal?</code>
195
+ </td>
196
+ <td>
197
+ Was this node produced by the matching of a nonterminal symbol?
198
+ </td>
199
+ <tr>
200
+ <td>
201
+ <code>text_value</code>
202
+ </td>
203
+ <td>
204
+ The substring of the input represented by this node.
205
+ </td>
206
+ <tr>
207
+ <td>
208
+ <code>elements</code>
209
+ </td>
210
+ <td>
211
+ Available only on nonterminal nodes, returns the nodes parsed by the elements of the matched sequence.
212
+ </td>
213
+ </tr>
214
+ </table></div></div></div><div id="bottom"></div></body></html>
@@ -0,0 +1,142 @@
1
+ <html><head><link rel="stylesheet" href="./screen.css" type="text/css"></link>
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>Documentation</li><li><a href="contribute.html">Contribute</a></li><li><a href="index.html">Home</a></li></ul></div></div><div id="middle"><div id="content"><div id="secondary_navigation"><ul><li>Syntax</li><li><a href="semantic_interpretation.html">Semantics</a></li><li><a href="using_in_ruby.html">Using In Ruby</a></li><li><a href="pitfalls_and_advanced_techniques.html">Advanced Techniques</a></li></ul></div><div id="documentation_content"><h1>Syntactic Recognition</h1>
9
+
10
+ <p>Treetop grammars are written in a custom language based on parsing expression grammars. Literature on the subject of <a href="http://en.wikipedia.org/wiki/Parsing_expression_grammar">parsing expression grammars</a> is useful in writing Treetop grammars.</p>
11
+
12
+ <h1>Grammar Structure</h1>
13
+
14
+ <p>Treetop grammars look like this:</p>
15
+
16
+ <pre><code>grammar GrammarName
17
+ rule rule_name
18
+ ...
19
+ end
20
+
21
+ rule rule_name
22
+ ...
23
+ end
24
+
25
+ ...
26
+ end
27
+ </code></pre>
28
+
29
+ <p>The main keywords are:</p>
30
+
31
+ <ul>
32
+ <li><p><code>grammar</code> : This introduces a new grammar. It is followed by a constant name to which the grammar will be bound when it is loaded.</p></li>
33
+ <li><p><code>rule</code> : This defines a parsing rule within the grammar. It is followed by a name by which this rule can be referenced within other rules. It is then followed by a parsing expression defining the rule.</p></li>
34
+ </ul>
35
+
36
+ <h1>Parsing Expressions</h1>
37
+
38
+ <p>Each rule associates a name with a <em>parsing expression</em>. Parsing expressions are a generalization of vanilla regular expressions. Their key feature is the ability to reference other expressions in the grammar by name.</p>
39
+
40
+ <h2>Terminal Symbols</h2>
41
+
42
+ <h3>Strings</h3>
43
+
44
+ <p>Strings are surrounded in double or single quotes and must be matched exactly.</p>
45
+
46
+ <ul>
47
+ <li><code>"foo"</code></li>
48
+ <li><code>'foo'</code></li>
49
+ </ul>
50
+
51
+ <h3>Character Classes</h3>
52
+
53
+ <p>Character classes are surrounded by brackets. Their semantics are identical to those used in Ruby's regular expressions.</p>
54
+
55
+ <ul>
56
+ <li><code>[a-zA-Z]</code></li>
57
+ <li><code>[0-9]</code></li>
58
+ </ul>
59
+
60
+ <h3>The Anything Symbol</h3>
61
+
62
+ <p>The anything symbol is represented by a dot (<code>.</code>) and matches any single character.</p>
63
+
64
+ <h2>Nonterminal Symbols</h2>
65
+
66
+ <p>Nonterminal symbols are unquoted references to other named rules. They are equivalent to an inline substitution of the named expression.</p>
67
+
68
+ <pre><code>rule foo
69
+ "the dog " bar
70
+ end
71
+
72
+ rule bar
73
+ "jumped"
74
+ end
75
+ </code></pre>
76
+
77
+ <p>The above grammar is equivalent to:</p>
78
+
79
+ <pre><code>rule foo
80
+ "the dog jumped"
81
+ end
82
+ </code></pre>
83
+
84
+ <h2>Ordered Choice</h2>
85
+
86
+ <p>Parsers attempt to match ordered choices in left-to-right order, and stop after the first successful match.</p>
87
+
88
+ <pre><code>"foobar" / "foo" / "bar"
89
+ </code></pre>
90
+
91
+ <p>Note that if <code>"foo"</code> in the above expression came first, <code>"foobar"</code> would never be matched.</p>
92
+
93
+ <h2>Sequences</h2>
94
+
95
+ <p>Sequences are a space-separated list of parsing expressions. They have higher precedence than choices, so choices must be parenthesized to be used as the elements of a sequence. </p>
96
+
97
+ <pre><code>"foo" "bar" ("baz" / "bop")
98
+ </code></pre>
99
+
100
+ <h2>Zero or More</h2>
101
+
102
+ <p>Parsers will greedily match an expression zero or more times if it is followed by the star (<code>*</code>) symbol.</p>
103
+
104
+ <ul>
105
+ <li><code>'foo'*</code> matches the empty string, <code>"foo"</code>, <code>"foofoo"</code>, etc.</li>
106
+ </ul>
107
+
108
+ <h2>One or More</h2>
109
+
110
+ <p>Parsers will greedily match an expression one or more times if it is followed by the star (<code>+</code>) symbol.</p>
111
+
112
+ <ul>
113
+ <li><code>'foo'+</code> does not match the empty string, but matches <code>"foo"</code>, <code>"foofoo"</code>, etc.</li>
114
+ </ul>
115
+
116
+ <h2>Optional Expressions</h2>
117
+
118
+ <p>An expression can be declared optional by following it with a question mark (<code>?</code>).</p>
119
+
120
+ <ul>
121
+ <li><code>'foo'?</code> matches <code>"foo"</code> or the empty string.</li>
122
+ </ul>
123
+
124
+ <h2>Lookahead Assertions</h2>
125
+
126
+ <p>Lookahead assertions can be used to give parsing expressions a limited degree of context-sensitivity. The parser will look ahead into the buffer and attempt to match an expression without consuming input.</p>
127
+
128
+ <h3>Positive Lookahead Assertion</h3>
129
+
130
+ <p>Preceding an expression with an ampersand <code>(&amp;)</code> indicates that it must match, but no input will be consumed in the process of determining whether this is true.</p>
131
+
132
+ <ul>
133
+ <li><code>"foo" &amp;"bar"</code> matches <code>"foobar"</code> but only consumes up to the end <code>"foo"</code>. It will not match <code>"foobaz"</code>.</li>
134
+ </ul>
135
+
136
+ <h3>Negative Lookahead Assertion</h3>
137
+
138
+ <p>Preceding an expression with a bang <code>(!)</code> indicates that the expression must not match, but no input will be consumed in the process of determining whether this is true.</p>
139
+
140
+ <ul>
141
+ <li><code>"foo" !"bar"</code> matches <code>"foobaz"</code> but only consumes up to the end <code>"foo"</code>. It will not match <code>"foobar"</code>.</li>
142
+ </ul></div></div></div><div id="bottom"></div></body></html>