How do I filter the sequence of tokens coming from my Lexer to my Parser when using Scala parser combinators?
Let me explain - suppose I have the fairly standard pattern of a Lexer (extending StdLexical
) and a Parser (extending StdTokenParsers
). The lexer turns a sequence of characters to a sequence of tokens, then the parser turns the sequence of tokens to an abstract syntax tree (of type Expr
).
I decide that some tokens, which could occur anywhere in the stream, I would like to have the option of filtering out, so I would like a function that would fit between the Lexer and Parser to remove these tokens. For example, I might want the lexer to tokenise comments, and then filter out these comments later.
What is the best way of writing this filter? This could use the parser combinator idiom, but doesn't have to.
Sample current code:
val reader = new PagedSeqReader(PagedSeq.fromReader(reader))
val tokens = new MyParser.lexical.Scanner(reader)
val parse = MyParser.phrase(parser)(tokens)
I would like to be able to write something like this:
val reader = new PagedSeqReader(PagedSeq.fromReader(reader))
val tokens = new MyParser.lexical.Scanner(reader)
val parse = MyParser.phrase(parser)(filter(tokens))