[Technique] An actual use for []

I found a use for [ ]!


Languages that use indentation for grouping are not context-free, but you 
can still parse them, just not in the general case (like the gedcom example 
recently, there's a limit to the nesting). In this sort of style:


list:  (                                word, newline, list1?)+.
list1: (                        indent, word, newline, list2?)+.
list2: (                indent, indent, word, newline, list3?)+.
list3: (        indent, indent, indent, word, newline, list4?)+.
list4: (indent, indent, indent, indent, word, newline        )+.
-indent: -"    ".
-newline: -#d?, -#a.
word: [L]*.


The problem with this is that if the nesting of the input is greater than 
what the grammar supports, you get an unhelpful message like:
   **** Parsing failed at line 13, position 17
                       oops    
                   ^
   **** Character: " ".
   **** Permitted at this position: 
        [L]*: [L]
        -#d?: #d
        newline: #a

So what I did was change the definition of list4 to:


list4: (indent, indent, indent, indent, word, newline, 
too-deeply-indented?)+.
too-deeply-indented: indent, indent, indent, indent, indent, [].

giving a slightly more helpful message:
   **** Parsing failed at line 13, position 21
                       oops    
                       ^
   **** Character: "o".
   **** Permitted at this position: 
        too-deeply-indented: []



Steven

Received on Sunday, 4 September 2022 12:15:45 UTC