Using Positional and positioned() in scala parser combinators

最后都变了- 提交于 2019-12-13 02:49:55

问题


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 ... ), ... )}

回答1:


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.



来源:https://stackoverflow.com/questions/20919068/using-positional-and-positioned-in-scala-parser-combinators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!