allison avatar allison committed 4d06086

Check for spacing after tokens, fix the position in the suffix rule,
add defensive checks for end of file.

Comments (0)

Files changed (1)

Spix/PEG/Parser.pir

   matched:
     $I0 = pos + 1
     self.'pos'($I0)
+    # Spacing is optional
+    $P0 = self.'Spacing'(input, $I0)
+
     tree = self.'new_node'('name'=>name, 'src'=>value, 'pos'=>pos)
     self.'set_memo'(pos, name, tree)
 
     .param int pos
     .local pmc tree, children, definitions
     .local int new_pos
-    new_pos = pos
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
 
     tree = self.'get_memo'(pos, 'Grammar')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
+    new_pos = pos
 
     # Spacing is optional
     $P0 = self.'Spacing'(input, pos)
     .param int pos
     .local pmc tree, children
     .local int new_pos
-    new_pos = pos
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
 
     tree = self.'get_memo'(pos, 'Definition')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
+    new_pos = pos
 
     # Identifier is required
     $P0 = self.'Identifier'(input, new_pos)
     .param int pos
     .local pmc tree, children, sequences
 
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Expression')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
     .param int pos
     .local pmc tree, children, definitions
 
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Sequence')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
     .param int pos
     .local pmc tree, named_children
 
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Prefix')
     unless null tree goto got_tree
     named_children = new ['Hash']
     .param string input
     .param int pos
     .local pmc tree, named_children
+    .local int new_pos
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
 
     tree = self.'get_memo'(pos, 'Suffix')
     unless null tree goto got_tree
     named_children = new ['Hash']
+    new_pos = pos
 
     # Primary is required
     $P0 = self.'Primary'(input, pos)
     if null $P0 goto got_tree
     named_children['Primary'] = $P0
+    new_pos = self.'pos'()
 
     # QUESTION, STAR, and PLUS are optional, but mutually exclusive
-    $P0 = self.'QUESTION'(input, pos)
+    $P0 = self.'QUESTION'(input, new_pos)
     if null $P0 goto no_question
     named_children['QUESTION'] = $P0
     goto done_alternation
   no_question:
-    $P0 = self.'STAR'(input, pos)
+    $P0 = self.'STAR'(input, new_pos)
     if null $P0 goto no_star
     named_children['STAR'] = $P0
   no_star:
-    $P0 = self.'PLUS'(input, pos)
+    $P0 = self.'PLUS'(input, new_pos)
     if null $P0 goto done_alternation
     named_children['PLUS'] = $P0
   done_alternation:
     .local pmc tree, named_children
     .local int matched
 
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Primary')
     unless null tree goto got_tree
     matched = 0
     .local pmc tree, named_children, conts
     .local string ident_string
     .local int new_pos
-    ident_string = ''
-    new_pos = pos
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
 
     tree = self.'get_memo'(pos, 'Identifier')
     unless null tree goto got_tree
     named_children = new ['Hash']
     conts = new ['ResizablePMCArray']
+    ident_string = ''
+    new_pos = pos
 
     # IdentStart is required
     $P0 = self.'IdentStart'(input, pos)
     .param int pos
     .local pmc tree
     .local string ident_string
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'IdentStart')
     unless null tree goto got_tree
     ident_string = ''
     .param int pos
     .local pmc tree
     .local string ident_string
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'IdentCont')
     unless null tree goto got_tree
 
-    $I0 = self.'length'()
-    if pos == $I0 goto got_tree
-
     # One of IdentStart or digit is required, mutually exclusive
     $P0 = self.'IdentStart'(input, pos)
     if null $P0 goto try_digit
     .local pmc tree, children
     .local string literal_string
     .local int new_pos
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Literal')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
     .param int pos
     .local pmc tree, children
     .local int new_pos
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Class')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
     .param int pos
     .local pmc tree, children
     .local int new_pos
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Range')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
     .local string char_string
     .local int new_pos
 
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Char')
     unless null tree goto got_tree
     char_string = ''
     .local pmc tree
     .local int new_pos
 
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'LEFTARROW')
     unless null tree goto got_tree
     new_pos = pos
     .local string src
     .local pmc children
     .local int new_pos
-    new_pos = pos
-    src = ''
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
 
     tree = self.'get_memo'(pos, 'Spacing')
     unless null tree goto got_tree
     children = new ['ResizablePMCArray']
+    new_pos = pos
+    src = ''
 
   children_loop:
     $P0 = self.'Space'(input, new_pos)
     .local pmc tree
     .local int new_pos
     .local string comment_string
+
+    $I0 = self.'length'()
+    if pos >= $I0 goto got_tree
+
     tree = self.'get_memo'(pos, 'Comment')
     unless null tree goto got_tree
     new_pos = pos
     .param string input
     .param int pos
     .local pmc tree
-    tree = self.'get_memo'(pos, 'Space')
-    unless null tree goto got_tree
 
     $I0 = self.'length'()
     if pos >= $I0 goto got_tree
 
+    tree = self.'get_memo'(pos, 'Space')
+    unless null tree goto got_tree
+
     $I0 = ord input, pos
     $S0 = chr $I0
 
     unless null tree goto got_tree
 
     $I0 = self.'length'()
-    if pos == $I0 goto matched
+    if pos >= $I0 goto matched
+
     goto got_tree
 
   matched:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.