Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional errors collecting #166

Open
alexander-myltsev opened this issue Apr 23, 2016 · 0 comments
Open

Optional errors collecting #166

alexander-myltsev opened this issue Apr 23, 2016 · 0 comments

Comments

@alexander-myltsev
Copy link
Contributor

There are cases when error collecting is not necessary. A user just'd like to know if parsing was successful or not. In meantime, error collecting is time consuming.

I suggest a patch as follows:

diff --git a/parboiled-core/src/main/scala/org/parboiled2/Parser.scala b/parboiled-core/src/main/scala/org/parboiled2/Parser.scala
index b306d5a..6d6788b 100644
--- a/parboiled-core/src/main/scala/org/parboiled2/Parser.scala
+++ b/parboiled-core/src/main/scala/org/parboiled2/Parser.scala
@@ -24,7 +24,8 @@ import shapeless._
 import org.parboiled2.support._

 abstract class Parser(initialValueStackSize: Int = 16,
-                      maxValueStackSize: Int = 1024) extends RuleDSL {
+                      maxValueStackSize: Int = 1024,
+                      collectErrors: Boolean = true) extends RuleDSL {
   import Parser._

   require(maxValueStackSize <= 65536, "`maxValueStackSize` > 2^16 is not supported") // due to current snapshot design
@@ -203,11 +204,15 @@ abstract class Parser(initialValueStackSize: Int = 16,
       if (phase0_initialRun())
         scheme.success(valueStack.toHList[L]())
       else {
-        val principalErrorIndex = phase1_establishPrincipalErrorIndex()
-        val p2 = phase2_establishReportedErrorIndex(principalErrorIndex)
-        val reportQuiet = phase3_determineReportQuiet(principalErrorIndex)
-        val parseError = phase4_collectRuleTraces(p2.reportedErrorIndex, principalErrorIndex, reportQuiet)()
-        scheme.parseError(parseError)
+        if (collectErrors) {
+          val principalErrorIndex = phase1_establishPrincipalErrorIndex()
+          val p2 = phase2_establishReportedErrorIndex(principalErrorIndex)
+          val reportQuiet = phase3_determineReportQuiet(principalErrorIndex)
+          val parseError = phase4_collectRuleTraces(p2.reportedErrorIndex, principalErrorIndex, reportQuiet)()
+          scheme.parseError(parseError)
+        } else {
+          scheme.failure(new Exception("Parser is not collecting errors. Set `collectErrors` to true to collect errors"))
+        }
       }
     } catch {
       case e: Parser.Fail ⇒
@@ -670,4 +675,4 @@ object ParserMacros {

     reify { ctx.Expr[RuleX](ruleTree).splice.asInstanceOf[Rule[I, O]] }
   }
-}
\ No newline at end of file
+}

Could you add it to code base?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant