代码之家  ›  专栏  ›  技术社区  ›  Nick Fortescue

在scala-StdLexical中学习新词?

  •  3
  • Nick Fortescue  · 技术社区  · 16 年前

    我正在尝试lex(然后解析)一种类似C的语言。在C中,有一些预处理器指令,其中换行符是重要的,而实际代码中的换行符只是空白。

    然而,我想知道是否有可能做到这一点,在一个单一的lexer。我对编写scala解析器combinator代码非常满意,但我不太确定如何编写 StdLexical

    有没有人能写一些简单的示例代码 #include

    1 回复  |  直到 16 年前
        1
  •  7
  •   Nick Fortescue    16 年前

    好吧,我自己解决了,给后人回答。

    override def token: CeeLexer.Parser[Token] = controlLine 
      // | ... (where ... is whatever you want to keep of the original method)
    def controlLine = hashInclude
    
    def hashInclude : CeeLexer.Parser[HashInclude] =
      ('#' ~ word("include") ~ rep(nonEolws)~'\"' ~ rep(chrExcept('\"', '\n', EofCh)) ~ '\"' ~ '\n' |
       '#' ~ word("include") ~ rep(nonEolws)~'<' ~ rep(chrExcept('>', '\n', EofCh)) ~ '>' ~ '\n' ) ^^ {
       case hash~include~whs~openQ~fname~closeQ~eol =>  // code to handle #include
     }
    
    推荐文章