parser combinator: how to terminate repetition on keyword

后端 未结 1 603
眼角桃花
眼角桃花 2021-01-06 14:25

I\'m trying to figure out how to terminate a repetition of words using a keyword. An example:

class CAQueryLanguage extends JavaTokenParsers {
    def expres         


        
相关标签:
1条回答
  • 2021-01-06 14:56

    Is this what you are looking for?

    import scala.util.parsing.combinator.syntactical._
    
    object CAQuery extends StandardTokenParsers {
        lexical.reserved += ("START", "END")
        lexical.delimiters += (" ")
    
        def query:Parser[Any]= "START" ~> rep1(ident) <~ "END"
    
        def parse(s:String) = {
           val tokens = new lexical.Scanner(s)
           phrase(query)(tokens)
       }   
    }
    
    println(CAQuery.parse("""START a END"""))       //List(a)
    println(CAQuery.parse("""START a b c END"""))   //List(a, b, c)
    

    If you would like more details, you can check out this blog post

    0 讨论(0)
提交回复
热议问题