简体   繁体   中英

Using Positional and positioned() in scala parser combinators

With separate Lexer and Parser ...

class YamlLexical extends StdLexical with YamlTokens with RegexParsers {...
object YamlParser extends StdTokenParsers with YamlTokens with PackratParsers {...

... how to get the position of the parsed string into AST classes?

(... positioned(elem(...)) * ... )^^ { ... => List( Ast(startpos, parsedtext, ... subnodes ... ), ... )}

The type of positioned is

def positioned[T <: Positional](p: ⇒ Parser[T]): Parser[T]

which means that the parsed elements must extend Positional . So, for example, YamlLexical should be:

class YamlLexical extends Positional 

in that way, any parsed element will automatically have a pos which records the position in which it was parsed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM