blob: 7aa6178df9a17f80835f8ec6d5d144df24f72404 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.util.parsing
package combinator
package syntactical
import token._
import scala.collection.mutable
import language.implicitConversions
/** This component provides primitive parsers for the standard tokens defined in `StdTokens`.
*
* @author Martin Odersky, Adriaan Moors
*/
trait StdTokenParsers extends TokenParsers {
type Tokens <: StdTokens
import lexical.{Keyword, NumericLit, StringLit, Identifier}
protected val keywordCache = mutable.HashMap[String, Parser[String]]()
/** A parser which matches a single keyword token.
*
* @param chars The character string making up the matched keyword.
* @return a `Parser` that matches the given string
*/
// implicit def keyword(chars: String): Parser[String] = accept(Keyword(chars)) ^^ (_.chars)
implicit def keyword(chars: String): Parser[String] =
keywordCache.getOrElseUpdate(chars, accept(Keyword(chars)) ^^ (_.chars))
/** A parser which matches a numeric literal */
def numericLit: Parser[String] =
elem("number", _.isInstanceOf[NumericLit]) ^^ (_.chars)
/** A parser which matches a string literal */
def stringLit: Parser[String] =
elem("string literal", _.isInstanceOf[StringLit]) ^^ (_.chars)
/** A parser which matches an identifier */
def ident: Parser[String] =
elem("identifier", _.isInstanceOf[Identifier]) ^^ (_.chars)
}
|