parboiled2 | A macro-based PEG parser generator for Scala
kandi X-RAY | parboiled2 Summary
kandi X-RAY | parboiled2 Summary
A macro-based PEG parser generator for Scala 2.10+
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of parboiled2
parboiled2 Key Features
parboiled2 Examples and Code Snippets
Community Discussions
Trending Discussions on parboiled2
QUESTION
I'm looking at parboiled2 in Scala and it's got an interesting way of encoding parser behavior in the type system using covariant/contravariant subtypes:
https://github.com/sirthias/parboiled2#rule-types-and-the-value-stack
I'm curious if something similar can be accomplished in C++ with template meta-programming.
I suspect the covariant behavior could be emulated with inheritance, but what about contravariant?
...ANSWER
Answered 2018-May-19 at 17:54In C++, like in other OO languages, subtypes are generally represented by inheritance.
However, we cannot model covariance and contravariance relationships through inheritance, because there is no way to list the base classes of a class (without any of the various reflection proposals, which have not made their way into the language yet).
The easiest way to allow this kind of behavior is to allow covariant and contravariant templated classes to convert based on the relationship of the related types.
CovarianceIf
Derived
is-aBase
, thenCovariant
"should-be-a"Covariant
.
Normally, the solution would be to make Covariant
inherit from Covariant
, but we currently have no way of finding Base
given only Derived
. However, we can enable the conversion by writing a constructor for Covariant
taking any Covariant
:
QUESTION
I'm trying to parse a file format, using the excellent parboiled2 library, in which the presence of some fields is dependent upon the value of one or more fields already processed.
For example, say I have two fields, the first of which is a flag indicating whether the second is present. That is, if the first field is true
, then the second field (which is an integer value, in this example) is present and must be processed - but if it's false
, then the second field isn't present at all. Note that this second field isn't optional - it either must be processed (if the first field is true
) or must not be processed (if the first field is false
).
So, if a third field (which we'll assume is always present) is a quoted string, both of the following lines are valid:
...ANSWER
Answered 2018-Feb-21 at 03:15I would do something like this:
QUESTION
What's the best way to report an error in a parser action in parboiled2 (I'm using v 2.1.4)?
For example, say I want to read an integer value and report an error if its not within the expected range? I tried calling fail
, but that doesn't appear to be valid within a parser action. Also, I can't tell how I should provide the stack value to the test
rule. Do I simply throw a ParseError
exception?
To be a little more specific, consider the following rule:
...ANSWER
Answered 2017-Apr-20 at 18:19You can use test
for that. The trick is that actions can also return a Rule
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install parboiled2
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page