S-expressions: Difference between revisions

→‎{{header|TXR}}: Explanation of complicated line.
(→‎{{header|TXR}}: Disclaimer about string literal lexing.)
(→‎{{header|TXR}}: Explanation of complicated line.)
Line 2,798:
@/\s*/@(atom e)@\
@(or)@\
@/\s*\(\s*/@(coll :vars (e))@(expr e)@/\s*/@(untillast))@(end))@\
@(end)@\
@(end)
Line 2,838:
 
TODO: Note that the recognizer for string literals does not actually process the interior escape sequences <code>\"</code>; these remain as part of the string data. The only processing is the stripping of the outer quotes from the lexeme.
 
Explanation of most confusing line:
 
<lang txr> @/\s*\(\s*/@(coll :vars (e))@(expr e)@/\s*/@(last))@(end)</lang>
 
First, we match an open parenthesis that can be embedded in whitespace. Then we have a <code>@(coll)</code> construct which terminates with <code>@(end)</code>. This is a repetition construct for collecting zero or more items. The <code>:vars (e)</code> argument makes the collect strict: each repetition must bind the variable <code>e</code>. More importantly, in this case, if nothing is
collected, then <code>e</code> gets bound to <code>nil</code> (the empty list). The collect construct does not look at context beyond itself. To terminate the collect at the closing parenthesis we use <code>@(last))</code>. The second closing parenthesis here is literal text to be matched, not TXR syntax. This special clause establishes the terminating context without which the collect will munge all input. When the last clause matches, whatever it matches is consumed and the collect ends. (There is a related <code>@(until)</code> clause which terminates the collect, but leaves its own match unconsumed.)
 
{{omit from|Brlcad}}
543

edits