scala - How to define a Regex in StandardTokenParsers to identify path? -


i writing parser in want parse arithmetic expressions like: /hdfs://xxx.xx.xx.x:xxxx/path1/file1.jpg+1 want parse change infix postfix , calculation. used helps part of code in discussion well.

 class infixtopostfix extends standardtokenparsers {  import lexical._   def regexstringlit(r: regex): parser[string] = acceptmatch(  "string literal matching regex " + r,  { case  stringlit(s)  if r.unapplyseq(s).isdefined => s })  def pathident: parser[string] =regexstringlit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)  lexical.delimiters ++= list("+","-","*","/", "^","(",")",",")  def value :parser[expr] = numericlit ^^ { s => number(s) } def variable:parser[expr] =  pathident ^^ { s => variable(s) } def parens:parser[expr] = "(" ~> expr <~ ")"  def argument:parser[expr] = expr <~ (","?) def func:parser[expr] = ( pathident ~ "(" ~ (argument+) ~ ")" ^^ { case f ~ _ ~ e ~ _ => function(f, e) })  def term = (value | parens | func | variable)  // needed define recursive because ^ right-associative def pow :parser[expr] = ( term ~ "^" ~ pow ^^ {case left ~ _ ~ right => binaryoperator(left, "^", right) }|             term) def factor = pow * ("*" ^^^ { (left:expr, right:expr) => binaryoperator(left, "*", right) } |                     "/" ^^^ { (left:expr, right:expr) => binaryoperator(left, "/", right) } ) def sum =  factor * ("+" ^^^ { (left:expr, right:expr) => binaryoperator(left, "+", right) } |                     "-" ^^^ { (left:expr, right:expr) => binaryoperator(left, "-", right) } ) def expr = ( sum | term )  def parse(s:string) = {     val tokens = new lexical.scanner(s)     phrase(expr)(tokens) } 

//and rest of code

i able solve following errors of this answer:

      scalaparser.scala:192: invalid escape character   [error]     def pathident: parser[string] =regexstringlit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)   [error]                                                               ^   [error] scalaparser.scala:192: invalid escape character   [error]     def pathident: parser[string] =regexstringlit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)    [error]                                                                ^    [error] scalaparser.scala:192: invalid escape character    [error]     def pathident: parser[string] =regexstringlit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)    [error]                                                                        ^ 

with change of pathident this:

  def pathident: parser[string] =regexstringlit("/hdfs://([\\d.]+):(\\d+)/([\\w/]+/(\\w+\\.w+))".r) 

now getting run time error says:

 [1.1] failure: string literal matching regex /hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+)) expected  /hdfs://111.33.55.2:8888/folder1/p.a3d+1 ^ 

it working using javatokenparsers current changes , had use standardtokenparsers.

in double quoted string backslash escape character. if mean use literal backslash in double quotes string must escape it, "\d" should "\\d".

furthermore not need escape regex dot within character class, since dot has no special meaning character class. "[\d.]" should "[\d.]".

you can forgo escaping business using raw interpolator or multi-line string literals using triple quotes.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -