2
* Copyright (C) 2009-2011 Mathias Doenitz
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
19
import common.FileUtils
24
import io.{Codec, Source}
25
import java.io.InputStream
26
import util.DynamicVariable
28
package object scala {
31
type Rule = rules.Rule
32
type PopRule1[-Z] = rules.PopRule1[Z]
33
type PopRule2[-Y, -Z] = rules.PopRule2[Y, Z]
34
type PopRule3[-X, -Y, -Z] = rules.PopRule3[X, Y, Z]
35
type ReductionRule1[-Z, +R] = rules.ReductionRule1[Z, R]
36
type ReductionRule2[-Y, -Z, +R] = rules.ReductionRule2[Y, Z, R]
37
type ReductionRule3[-X, -Y, -Z, +R] = rules.ReductionRule3[X, Y, Z, R]
38
type Rule0 = rules.Rule0
39
type Rule1[+A] = rules.Rule1[A]
40
type Rule2[+A, +B] = rules.Rule2[A, B]
41
type Rule3[+A, +B, +C] = rules.Rule3[A, B, C]
42
type Rule4[+A, +B, +C, +D] = rules.Rule4[A, B, C, D]
43
type Rule5[+A, +B, +C, +D, +E] = rules.Rule5[A, B, C, D, E]
44
type Rule6[+A, +B, +C, +D, +E, +F] = rules.Rule6[A, B, C, D, E, F]
45
type Rule7[+A, +B, +C, +D, +E, +F, +G] = rules.Rule7[A, B, C, D, E, F, G]
46
type CharRule = rules.CharRule
48
type ParseRunner[V] = scala.parserunners.ParseRunner[V]
49
type BasicParseRunner[V] = scala.parserunners.BasicParseRunner[V]
50
val BasicParseRunner = scala.parserunners.BasicParseRunner
51
type RecoveringParseRunner[V] = scala.parserunners.RecoveringParseRunner[V]
52
val RecoveringParseRunner = scala.parserunners.RecoveringParseRunner
53
type ReportingParseRunner[V] = scala.parserunners.ReportingParseRunner[V]
54
val ReportingParseRunner = scala.parserunners.ReportingParseRunner
55
type TracingParseRunner[V] = scala.parserunners.TracingParseRunner[V]
56
val TracingParseRunner = scala.parserunners.TracingParseRunner
59
* Creates an "AND" syntactic predicate according to the PEG formalism.
61
def &(sub: scala.Rule): Rule0 = new TestMatcher(sub.matcher).label("Test")
64
* Groups the given sub rule into one entity so that a following ~> operator receives the text matched by the whole
65
* group rather than only the immediately preceding sub rule.
67
def group[T <: scala.Rule](rule: T): T = rule.matcher match {
68
case x: SequenceMatcher => { x.defaultLabel("<group>"); rule }
69
case x: FirstOfMatcher => { x.defaultLabel("<group>"); rule }
70
case _ => throw new IllegalArgumentException("group(...) can only be applied to sequences (~) or choices (|)")
74
* A rule that always matches but consumes no input.
76
lazy val EMPTY: Rule0 = new EmptyMatcher()
79
* A rule that matches any single character except EOI.
81
lazy val ANY: Rule0 = new AnyMatcher()
84
* A rule that matches the End-Of-Input non-character.
86
lazy val EOI: Rule0 = new CharMatcher(Chars.EOI)
89
* A rule that matches the "INDENT" non-character as produced by the IndentDedentInputBuffer.
91
lazy val INDENT: Rule0 = new CharMatcher(Chars.INDENT)
94
* A rule that matches the "DEDENT" non-character as produced by the IndentDedentInputBuffer.
96
lazy val DEDENT: Rule0 = new CharMatcher(Chars.DEDENT)
99
* A rule that never matches anything (i.e. that always fails).
101
lazy val NOTHING: Rule0 = new NothingMatcher()
104
* A parser action removing the top element from the value stack.
106
lazy val DROP: PopRule1[Any] = new ActionMatcher(action(ok(stack1[Any](Pop)))).label(nameAction("Drop"))
109
* A parser action removing the top two elements from the value stack.
111
lazy val DROP2: PopRule2[Any, Any] = new ActionMatcher(action(ok(stack2[Any, Any](Pop)))).label(nameAction("Drop2"))
114
* A parser action removing the top three elements from the value stack.
116
lazy val DROP3: PopRule3[Any, Any, Any] =
117
new ActionMatcher(action(ok(stack3[Any, Any, Any](Pop)))).label(nameAction("Drop3"))
119
type RuleMethod = StackTraceElement
121
def make[A, U](a: A)(f: A => U): A = { f(a); a }
123
private[scala] def getCurrentRuleMethod: StackTraceElement = {
127
case t: Throwable => t.getStackTrace()(3)
131
implicit def toTestAction(f: Context[Any] => Boolean): Rule0 = new ActionMatcher(new Action[Any] {
132
def run(context: Context[Any]): Boolean = f(context)
133
}).label("TestAction")
135
implicit def toRunAction(f: Context[Any] => Unit): Rule0 = new ActionMatcher(new Action[Any] {
136
def run(context: Context[Any]): Boolean = {f(context); true}
137
}).label("RunAction")
139
implicit def creator4PopRule1[Z](m: Matcher): PopRule1[Z] = new PopRule1[Z](m)
140
implicit def creator4PopRule2[Y, Z](m: Matcher): PopRule2[Y, Z] = new PopRule2[Y, Z](m)
141
implicit def creator4PopRule3[X, Y, Z](m: Matcher): PopRule3[X, Y, Z] = new PopRule3[X, Y, Z](m)
142
implicit def creator4ReductionRule1[Z, R](m: Matcher): ReductionRule1[Z, R] = new ReductionRule1[Z, R](m)
143
implicit def creator4ReductionRule2[Y, Z, R](m: Matcher): ReductionRule2[Y, Z, R] = new ReductionRule2[Y, Z, R](m)
144
implicit def creator4ReductionRule3[X, Y, Z, R](m: Matcher): ReductionRule3[X, Y, Z, R] = new ReductionRule3[X, Y, Z, R](m)
145
implicit def creator4Rule0(m: Matcher): Rule0 = new Rule0(m)
146
implicit def creator4Rule1[A](m: Matcher): Rule1[A] = new Rule1[A](m)
147
implicit def creator4Rule2[A, B](m: Matcher): Rule2[A, B] = new Rule2[A, B](m)
148
implicit def creator4Rule3[A, B, C](m: Matcher): Rule3[A, B, C] = new Rule3[A, B, C](m)
149
implicit def creator4Rule4[A, B, C, D](m: Matcher): Rule4[A, B, C, D] = new Rule4[A, B, C, D](m)
150
implicit def creator4Rule5[A, B, C, D, E](m: Matcher): Rule5[A, B, C, D, E] = new Rule5[A, B, C, D, E](m)
151
implicit def creator4Rule6[A, B, C, D, E, F](m: Matcher): Rule6[A, B, C, D, E, F] = new Rule6[A, B, C, D, E, F](m)
152
implicit def creator4Rule7[A, B, C, D, E, F, G](m: Matcher): Rule7[A, B, C, D, E, F, G] = new Rule7[A, B, C, D, E, F, G](m)
154
implicit def charArray2Input(input: Array[Char]): Input = new Input(input)
155
implicit def string2Input(input: String): Input = new Input(input.toCharArray)
156
implicit def source2Input(input: Source): Input = new Input(input.toArray[Char])
157
implicit def inputStream2Input(input: InputStream)(implicit codec: Codec): Input =
158
new Input(FileUtils.readAllChars(input, codec.charSet))
162
private val currentRuleLabel = new DynamicVariable[String](null)
163
private val currentActionIndex = new DynamicVariable[Int](0)
165
private[scala] def withCurrentRuleLabel[A](s: String)(f: => A): A =
166
currentRuleLabel.withValue(s) {
167
currentActionIndex.withValue(0) {
172
private[scala] def nameAction(coreLabel: String) = {
173
currentActionIndex.value += 1
174
currentRuleLabel.value + coreLabel + "Action" + currentActionIndex.value
b'\\ No newline at end of file'