3 ms·
Let's say you have one language that supports double-quoted strings and interpolates local variables, like this: x = 17 print "The number is $x" produ
by thristian 12y ago
Let's say you have one language that supports double-quoted strings and interpolates local variables, like this:
x = 17
print "The number is $x"
produces:
The number is 17
Let's say in some situation you don't want interpolation, and so you want to mix in some other language that supports interpolation-less strings, and it just happens to use triple-quoted strings as the syntax for that behaviour. So, we make a new language by combining the original language's expression syntax with the new language's expressions syntax using the ordered choice operator. Then we feed it this input:
x = 17
print """The number is $x"""
This time, the first language's parser sees the opening quote, then immediately afterward it sees a closing quote. Then it sees another opening quote, some text, an interpolation and a closing quote, then lastly another opening and closing quote, so it yields three string literals and concatenates them to produce one large, interpolated string.
Even though our combined grammar had a special rule for triple-quoted strings, we couldn't use it just by blindly concatenating parts of the grammar; we'd have to rewrite both of them a little bit to get them to mesh nicely. Granted, the above is a slightly contrived example, but most non-trivial PEG grammars have to pay at least some attention to the order of their ordered choice operands, even within a single language.
- sklogic 12y agoI still don't understand what's the problem here. User must be aware of the order of a choice and insert his language extension accordingly. Paying some attention to what you're doing never hurts anyway, and it's hardly a blocking issue barring PEG adoption. I normally do this sort of things by providing a number of extension points in the grammar, in the beginning, middle and the end of a semantic entity, instead of simply concatenating grammars (e.g., see such extension points here: https://github.com/combinatorylogic/clike/blob/master/clike/clike-parser.hl https://github.com/combinatorylogic/clike/blob/master/clike/... )