Vogon Jeltz
Vogon Jeltz

Reputation: 1315

Scala Parser Combinators Matching Wrong Parser

I am writing an extremely simple programming language using scala's parsers. I am trying to allow users to have multi-word variables ie my variable and I want to allow them to assign variables in three ways: let var = 2, var =2, set var to 4. I have the first two working, but I cant get the latter to work.

Here is my code

      lazy val line:PackratParser[Line] = (assignment | element | comment) <~ (";" | "[\n\r]*".r)

      lazy val assignment:PackratParser[Assignment] = assignmentLHS ~ element ^^ {
        case x ~ y => new Assignment(x,y)
      }

      lazy val assignmentLHS =  "set" ~ "[" ~> identifier <~ "]" ~ "to" | ("let"?) ~> identifier <~ "="

      lazy val identifier:Parser[String] = "([a-zA-Z]+[a-zA-Z0-9]* ?)+".r ^^ (X => if (X.charAt(X.length-1) == ' ') X.substring(0, X.length-1) else X)

     lazy val element:PackratParser[Element] =(
        functionDef
      | comparison
      | expression
      | boolean
      | controlStatement
      | functionCall
      | reference
      | value
      | codeBlock

    )
    lazy val reference: PackratParser[Reference] = identifier ^^ (x=>new Reference(x))

Elements are most things in the language.

I would like to replace the assignmentLHS parser with:

  lazy val assignmentLHS =  "set" ~> identifier <~ "to" | ("let"?) ~> identifier <~ "="

so that the user can write set my variable to 4 instead of `set [my variable] to 4. The problem is that it just parses that as a reference

Upvotes: 0

Views: 68

Answers (1)

WestCoastProjects
WestCoastProjects

Reputation: 63211

Why do you have a space in the identifier before the 'optional' '?' symbol:

0-9]* ?)+

Maybe that is causing

set <identifier>

to change to

set other_chars

and set becomes part of the identifier. That would explain why your use of

"set" ~ "[" ~> identifier <~ "]"

works but

"set" ~> identifier <~ "to" 

does not

Upvotes: 0

Related Questions