Comments (3)
I would also like this! I'll start researching & brainstorming for how to get it added
from bnf.
I've been giving this some thoughts, and there are two questions to answer before solving this
How to report faulty input error?
the current API has no way to report errors (which it probably should! 😅)
// this returns an iterator! but no Result
let parsed = grammar.parse_input(sentence);
Changing this would be a breaking change, but a Result
could return Error
with the faulty input text offset.
Where in the faulty input to report?
The current parser uses Earley parsing. There are many different parsing attempts for most grammars, each of which get differing levels of "success" parsing the input text.
A "good first try" solution to this might be "What parsing got the farthest in the input text?". There will be times when this reporting is naive, or another parse tree more useful, but it seems like a simple strategy that can be implemented.
Solutions
So with those questions in mind, what options does BNF have?
Optional "longest match" Parsing
A non-breaking change would be to allow ParseTree
to be possibly incomplete, if opted into that behavior.
let mut parse_trees = grammar.parse_input(sentence);
parse_trees.report_incomplete(Grammar::parse::Longest);
// now when parsing is being done, the parser will record the longest incomplete match
for tree in parse_trees {
// and tree.is_complete will tell if the parse tree is the complete input, or only partial
}
Include In Error
A breaking change, but a new API could be added that is more Error
verbose, and includes the longest partial match.
let mut parser = grammar.parser();
let parse_trees: Iterator<Result<ParseTree, ParseError>> = parser.parse_input(sentence);
for parse_tree in parse_trees {
match parse_tree {
Ok(tree) => /* full success parse tree */,
Err(parse_error) => /* parse_error will have the input offset, and what BNF production was being used */
}
}
Thank You
Thanks for bringing this up! I definitely see the use case, and want it for my own reasons. Let me know if any of the options look appealing, or if there is another option you'd like considered.
from bnf.
great proposals! personally I prefer the idea of having a result with an error so I have to deal with it if there's a problem parsing the whole input, but would be glad for any more input around use cases / needs from the lib.
the question I find I still have is should this error be the sort of thing the a user writing the input should know what to do with? or instead something the developer writing an app that uses bnf
knows how to handle and report to their user. I can see some convenience in the former, but maybe more flexibility in developing using bnf
in the latter 🤔.
Maybe in some cases the developer is the same as the user, but I'm thinking in terms of maybe someone working on a compiler or text input handler... or even this crate 😄 for example, I appreciate the errors nom
gives me but they're definitely stuff I don't want to pass on to people using bnf
when possible (though there may be some coming through).
from bnf.
Related Issues (20)
- 2 Questions about Parsing HOT 5
- Empty String Rules Fail to Match HOT 4
- Empty String Rules (Still) Fail to Match HOT 7
- faster production matching while earley parsing
- reusable grammar parser
- Nullable productions (Still, Still) Fail to Match HOT 6
- Right-recursive productions Fail to Match
- Broken coverage report HOT 6
- How to use the bnf lib in sql parse function HOT 7
- Undefined nonterminal is accepted in bnf parsing HOT 2
- Order of Rules Affects Parsing HOT 2
- compare Criterion & Divan HOT 1
- Rethink Grammar::generate logic HOT 5
- Generate code coverage reports as a step in our GitHub actions HOT 1
- Nullable Rules?
- suggestion: extract bnf test-cases to their own .bnf files? HOT 3
- Ability to parse BNF grammars at compile time HOT 3
- Iterator::size_hint
- Include README.md as documentation in lib.rs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bnf.