Skip to content

Optional errors collecting  #166

@alexander-myltsev

Description

@alexander-myltsev

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions