Comments (6)
Couldn't you get the list of error messages on a failure, or are you entirely reliant on the stringified results of show :: ParseError -> String
?
Example:
case result of
Left x -> f $ errorMessages x
Right y -> g y
where
f = undefined -- Your code to operate on the errors
g = undefined -- Your code to operate on successful parse
from megaparsec.
Yes, that's what I'm doing. But if you look into how to implement f
, it turns out to be unnecessarily complicated. I need to write something like this:
Left err ->
let pos = errorPos err
name = sourceName pos
line = sourceLine pos
column = sourceColumn pos
messages = showErrorMessages
"or" "unknown parse error" "expecting"
"unexpected" "end of input"
(errorMessages err)
in intercalate ":" [name, show line, show column, "Syntax Error"] ++
(unlines $ map (" " ++) $ lines $ message)
That's relatively complicated for such a simple task, and I have to lookup how to call showErrorMessage
in the sources as well as figure out the API for source positions.
from megaparsec.
@Toxaris, rendering of error messages (i.e. everything except for the first line with file name, line number, and column number) is done with showMessages
function, which is hopefully more handy than Parsec's showErrorMessages
. Its type looks like this:
showMessages :: [Message] -> String
When I first saw showErrorMessages
it made me choke. Absolutely naïve idea of passing words into this function in hope to make it “international”. Has it even occurred to the author of Parsec that translation into different language is not done by one-to-one word substitution? Even if we consider only punctuation, list separated by commas with “or” / “and” at the end looks differently in different languages. I only speak three languages: Russian, English, and French and it's different in them all (punctuation); I can imagine this approach is even less applicable for eastern languages like Arabic and Japanese. So, let's focus on English alone for now.
What you're asking is worth consideration. That part about better default representation is. I don't see how I can make easier custom rendering of error messages. If you want it, you'll obviously have to write some Haskell.
Although I use Emacs on daily basis I don't know whether it expects quoted file names when they contain space…
from megaparsec.
Oh showMessages
sounds like a nice improvement indeed. Sorry for not checking out what you already improved before posting here.
A better default would still help me. Maybe there is a standard format that is understood by many tools, not just emacs?
from megaparsec.
@Toxaris, The format has been changed in b062a39, the new default should be satisfactory.
from megaparsec.
thanks 👍
from megaparsec.
Related Issues (20)
- [proposed labels: question, feature request] best practices for stateful matching of simple patterns HOT 4
- Question: mergeError HOT 2
- Greedy combinators HOT 11
- Problematic `IsString` instance HOT 4
- Processing input prior to parsing while retaining source positions HOT 1
- Mention in documentation that `parse` is an alias for `runParser` HOT 1
- MonadAccum instance for ParsecT HOT 5
- Tabs are not handled correctly when errors are rendered HOT 4
- Indentation error lost in alternative HOT 3
- `local` clears all hints HOT 5
- 9.4 migration: getSourcePos now requires `Monad m =>` HOT 6
- Remove/upgrade version bounds of executable `test-debug` from megaparsec-tests HOT 2
- Add `drop` to `Text.Megaparsec` HOT 3
- Wrong source locations on `unexpected end of input` with custom tokens HOT 2
- get col, row (as Int's) and error message of the first TrivialError HOT 2
- deepseq-1.5? HOT 1
- Error context with additional source positions HOT 4
- Parsing hexadecimal floats HOT 1
- Rewrite rules for parser primitives HOT 1
- How to use `Operator` from tutorial 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 megaparsec.