~ubuntu-branches/ubuntu/wily/parboiled/wily-proposed

« back to all changes in this revision

Viewing changes to parboiled-scala/src/main/scala/org/parboiled/scala/package.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2014-11-10 21:10:42 UTC
  • Revision ID: package-import@ubuntu.com-20141110211042-wcmfz25icr5ituj5
Tags: upstream-1.1.6
ImportĀ upstreamĀ versionĀ 1.1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009-2011 Mathias Doenitz
 
3
 *
 
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
 
7
 *
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
package org.parboiled
 
18
 
 
19
import common.FileUtils
 
20
import matchers._
 
21
import scala._
 
22
import rules.Rule._
 
23
import support.Chars
 
24
import io.{Codec, Source}
 
25
import java.io.InputStream
 
26
import util.DynamicVariable
 
27
 
 
28
package object scala {
 
29
 
 
30
  // type aliases
 
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
 
47
  
 
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
 
57
 
 
58
  /**
 
59
   * Creates an "AND" syntactic predicate according to the PEG formalism.
 
60
   */
 
61
  def &(sub: scala.Rule): Rule0 = new TestMatcher(sub.matcher).label("Test")
 
62
 
 
63
  /**
 
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.
 
66
   */
 
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 (|)")
 
71
  }
 
72
 
 
73
  /**
 
74
   * A rule that always matches but consumes no input.
 
75
   */
 
76
  lazy val EMPTY: Rule0 = new EmptyMatcher()
 
77
 
 
78
  /**
 
79
   * A rule that matches any single character except EOI.
 
80
   */
 
81
  lazy val ANY: Rule0 = new AnyMatcher()
 
82
 
 
83
  /**
 
84
   * A rule that matches the End-Of-Input non-character.
 
85
   */
 
86
  lazy val EOI: Rule0 = new CharMatcher(Chars.EOI)
 
87
 
 
88
  /**
 
89
   * A rule that matches the "INDENT" non-character as produced by the IndentDedentInputBuffer.
 
90
   */
 
91
  lazy val INDENT: Rule0 = new CharMatcher(Chars.INDENT)
 
92
 
 
93
  /**
 
94
   * A rule that matches the "DEDENT" non-character as produced by the IndentDedentInputBuffer.
 
95
   */
 
96
  lazy val DEDENT: Rule0 = new CharMatcher(Chars.DEDENT)
 
97
 
 
98
  /**
 
99
   * A rule that never matches anything (i.e. that always fails).
 
100
   */
 
101
  lazy val NOTHING: Rule0 = new NothingMatcher()
 
102
 
 
103
  /**
 
104
   * A parser action removing the top element from the value stack.
 
105
   */
 
106
  lazy val DROP: PopRule1[Any] = new ActionMatcher(action(ok(stack1[Any](Pop)))).label(nameAction("Drop"))
 
107
 
 
108
  /**
 
109
   * A parser action removing the top two elements from the value stack.
 
110
   */
 
111
  lazy val DROP2: PopRule2[Any, Any] = new ActionMatcher(action(ok(stack2[Any, Any](Pop)))).label(nameAction("Drop2"))
 
112
 
 
113
  /**
 
114
   * A parser action removing the top three elements from the value stack.
 
115
   */
 
116
  lazy val DROP3: PopRule3[Any, Any, Any] =
 
117
    new ActionMatcher(action(ok(stack3[Any, Any, Any](Pop)))).label(nameAction("Drop3"))
 
118
 
 
119
  type RuleMethod = StackTraceElement
 
120
  
 
121
  def make[A, U](a: A)(f: A => U): A = { f(a); a }
 
122
 
 
123
  private[scala] def getCurrentRuleMethod: StackTraceElement = {
 
124
    try {
 
125
      throw new Throwable
 
126
    } catch {
 
127
      case t: Throwable => t.getStackTrace()(3)
 
128
    }
 
129
  }
 
130
 
 
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")
 
134
 
 
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")
 
138
 
 
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)
 
153
 
 
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))
 
159
 
 
160
  // helper methods
 
161
 
 
162
  private val currentRuleLabel = new DynamicVariable[String](null)
 
163
  private val currentActionIndex = new DynamicVariable[Int](0)
 
164
 
 
165
  private[scala] def withCurrentRuleLabel[A](s: String)(f: => A): A =
 
166
    currentRuleLabel.withValue(s) {
 
167
      currentActionIndex.withValue(0) {
 
168
        f
 
169
      }
 
170
    }
 
171
 
 
172
  private[scala] def nameAction(coreLabel: String) = {
 
173
    currentActionIndex.value += 1
 
174
    currentRuleLabel.value + coreLabel + "Action" + currentActionIndex.value
 
175
  }
 
176
 
 
177
}
 
 
b'\\ No newline at end of file'