Peter Stuifzand

Changes to the heredoc parser example

Jeffrey Kegler, the author of Marpa, was kind enough to suggest a few changes to the parser from the previous blog post.

Two of the three regular expressions were replaced with calls to the Marpa recognizer. The first one was replaced with a call to the pause_span method, which returns the start and length of the lexeme that caused the pause. Passing these two values to the literal method which returns the substring from the input string. This removes the need to mess around with the pos() from the input string.

The second regular expression that was replaced is the one that matches the newline. There is no need to match with this regex, because we already know at which lexeme the parser was paused with a call to the pause_lexeme method. This method will return ’newline’ when that parser found a newline. We only need to resume the parser in the right position.

These two changes makes the code simpler, because we don’t have to change the pos() for the input string every time we want to match with the regexes.

© 2023 Peter Stuifzand