Skip to content

Commit

Permalink
Remove regexnows method since it now requires more duplication of Sca…
Browse files Browse the repository at this point in the history
…la parser combinator code

- in the longer term we are trying to have the parser combinator library support more flexile whitespace handling
scala/scala-parser-combinators#25
  • Loading branch information
inkytonik committed Jul 25, 2014
1 parent f094525 commit b6e7f77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
14 changes: 10 additions & 4 deletions library/src/org/kiama/example/lambda3/Lambda.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,16 @@ trait SyntaxAnalyser extends PositionedParserUtilities {
name ^^ Var

lazy val name =
"[a-zA-Z]+".r ~ regexnows ("[0-9]*".r) ^^ {
case base ~ index =>
Name (base, if (index.isEmpty) None else Some (index.toInt))
}
"[a-zA-Z]+[0-9]+".r ^^ (
fullname => {
val (base, index) = fullname.span (_.isLetter)
Name (base, Some (index.toInt))
}
) |
"[a-zA-Z]+".r ^^ (
base =>
Name (base, None)
)

}

Expand Down
26 changes: 0 additions & 26 deletions library/src/org/kiama/util/ParserUtilities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,32 +119,6 @@ trait ParserUtilities extends RegexParsers with PackratParsers {
}
}

/**
* Create a parser that matches a regex string, but doesn't skip whitespace
* first. This operation is useful if you want to recognise parts of a lexical
* symbol with different regular expressions so you can use the parts
* separately. Otherwise you have to parse with one regex and then split the
* resulting string to get at its parts. Based on `RegexParser.regex` in the
* Scala library.
*/
def regexnows (r : Regex) : Parser[String] =
Parser { in =>
val source = in.source
val start = in.offset
(r findPrefixMatchOf (source.subSequence (start, source.length))) match {
case Some (matched) =>
Success (source.subSequence (start, start + matched.end).toString,
in.drop (matched.end))
case None =>
val found =
if (start == source.length ())
"end of source"
else
s"`${source.charAt (start)}'"
Failure (s"string matching regex `$r' expected but $found found", in)
}
}

/**
* Convenience conversion to lift parsers that return 2-tilde-tuples to parsers
* that return regular 2-tuples.
Expand Down

0 comments on commit b6e7f77

Please sign in to comment.