Parsect | Parser Combinator for TypeScript | Parser library
kandi X-RAY | Parsect Summary
kandi X-RAY | Parsect Summary
Parser Combinator for TypeScript
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 Parsect
Parsect Key Features
Parsect Examples and Code Snippets
Community Discussions
Trending Discussions on Parsect
QUESTION
It might seem that this question is a duplicate of this question, however either Parsec or the Indent library has changed since 2012 and none of the old examples I have found for the indent library compile with the latest versions.
I want to make a parser for a programming language where indentation is part of the syntax (used to indicate scopes), in order to achieve this I want to make use of the Text.Parsec.Indent library, but I am at a loss on how to use it. It is clear to me that some modifications/custom parser type has to be made, but my limited knowledge on the State monad and surface level understanding of parsec seem to not be enough.
Let's say you wanted to make a parser for a simple list of ints like below. How would one achieve this?
...ANSWER
Answered 2021-Jun-25 at 14:04Okay after some deep diving into the source code and looking at the tests in the indents GitHub repository I managed to create a working example.
The following code can parse a simple indented list:
QUESTION
After doing
...ANSWER
Answered 2021-May-12 at 16:58You can define this with (<$) :: Functor f => a -> f b -> f a
. This thus performs a functor mapping with x <$ u = fmap (const x) u
:
QUESTION
I have little experience with algebraic data types, because I work in a language without native support. Usually one can use continuation passing style to get a remotely similar experience, but the handling of CPS encoded types is less comfortable.
Considering this why would a library like Parsec use CPS?
...ANSWER
Answered 2021-Jan-18 at 22:07This change was made March 2, 2009 in commit a98a3ccb by Antoine Latter. The commit comment was just:
QUESTION
I have a file parser that uses permute
from parsec
to combine various Parser
s. It looks like permute
only allows the Identity
monad, which I'm sure cuts down on the algorithmic complexity.
Now I'm trying to convert all of my parsing code to use MyErrorClass m => ParsecT String () m
, where Monad m => MyErrorClass m
is a class
with special error handling. Specifically, the code that uses permute
now needs to combine parsers that require MyErrorClass m
so that they can provide structured errors rather than just using fail
. (permute
is used only when parsing module config files for a programming language. The file format allows embedding of some language constructs, reusing parsers from the compiler.)
I only use permute
in 3 places and that number isn't likely to increase, so it isn't out of the question to hack together some sort of one-off function for each of the 3. Alternatively, I can just remove the flexibility in field ordering, but I'd rather not.
Before I do anything major, am I missing a simple solution in parsec
? Maybe I can somehow stuff ParsecT String () m
into permute
from parsers
as opposed to parsec
? (This would add a dependency, but might be worth it.)
Edit:
Here is some background regarding why structured errors are useful for parsing.
For the most part, I do parsing and validation separately. For example, checking for duplicate symbol definitions happens after parsing succeeds.
I frequently get parsing errors similar to expected //, /* or argument type
, and it can take a while (for me, the language creator) to understand what parser is being invoked.
MyErrorClass
allows structuring of error messages, e.g.
ANSWER
Answered 2020-Dec-06 at 23:12As @danidiaz suggested, parser-combinators
easily solves the literal problem of permuting ParsecT
.
The caveat is that using ParsecT
with a Monad m
that transforms errors (even its own) isn't particularly useful, other than being able to use fail-fast errors that can't be ignored by <|>
and try
.
You can't "demote" the error state of the
ParsecT
tom
's own type of error because (AFAIK) there is no way to access the current error state of theParsecT
.ParsecT
lacks something comparable toStateT
'smapStateT
. This means that there is no way to transform an error stored inm
without actually executing the parser withrunPT
(etc.), which isn't possible to do mid-parse.
In summary, ParsecT
doesn't leak m
in the same way that StateT
does, making some of m
's functionality inaccessible from within ParsecT
.
QUESTION
I was writing some Haskell earlier today. Came up with something along the lines of
...ANSWER
Answered 2020-Oct-26 at 03:26Parsec is a backtracking monad. While the MonadWriter
instance you propose is not impossible, it's a strange thing to put a Writer
underneath a backtracking monad. This is because the inner layers of a monad stack provide the "foundational" effects upon which the outer layers build -- that is, whatever ParsecT u (Writer w)
does, it must do so only in terms of tell
. So if it tell
s a value in a branch that is later backtracked out of, there is no possibility to "untell" that value, and so the Writer
's result will be more like a trace of the parsing algorithm rather than a trace of the dataflow abstraction that Parsec is presenting. It's not useless, but it's pretty odd, and you would have much more natural semantics if WriterT
were on the outside and Parsec
on the inside.
The same argument applies to State
. Parsec exposes both its own user state functionality (the u
parameter) and a MonadState
instance that inherits the underlying monad's state functionality. The latter comes with the same caveats as MonadWriter
-- the statefulness follows the trace of the algorithm, not the dataflow. I don't know why this instance was included while the Writer
instance was not; they are both tricky in the same way.
QUESTION
Parsec provides an operator to choose between two parsers:
...ANSWER
Answered 2020-Sep-07 at 15:49Try this:
QUESTION
I am trying to get more familiar with megaparsec, and I am running into some issues with presedences. By 'nested data' in the title I refer to the fact that I am trying to parse types, which in turn could contain other types. If someone could explain why this does not behave as I would expect, please don't hesitate to tell me.
I am trying to parse types similar to those found in Haskell. Types are either base types Int
, Bool
, Float
or type variables a
(any lowercase word).
We can also construct algebraic data types from type constructors (Uppercase words) such as Maybe
and type parameters (any other type). Examples are Maybe a
and Either (Maybe Int) Bool
. Functions associate to the right and are constructed with ->
, such as Maybe a -> Either Int (b -> c)
. N-ary tuples are a sequence of types separated by ,
and enclosed in (
and )
, such as (Int, Bool, a)
. A type can be wrapped in parenthesis to raise its precedence level (Maybe a)
. A unit type ()
is also defined.
I am using this ADT to describe this.
...ANSWER
Answered 2020-Aug-24 at 07:47Your code does not handle precedences at all, and also as a result of this it uses looping left-recursion.
To give an example of left-recursion in your code, pFunctionType
calls pType
as the first action, which calls pFunctionType
as the first action. This is clearly a loop.
For precedences, I recommend to look at tutorials on "recursive descent operator parsing", a quick Google search reveals that there are several of them. Nevertheless I can summarize here the key points. I write some code.
QUESTION
I want to access parsecs input stream directly which can be done using getParserState
. To read from the stream the uncons
method is provided. However I'm facing (as usual) a type related problem. so this is my parsing function:
ANSWER
Answered 2020-Jan-09 at 12:22Here’s what’s happening: uncons
needs to run in some sort of monad. You can see this by its type signature: uncons :: Stream s m t => s -> m (Maybe (t, s))
. But you’re just doing let x = uncons i
— so x
is a monadic computation which returns the result of uncons i
, but you haven’t specified which monad this monadic computation is running within. You happen to know that you ‘want’ uncons i
to run within your monad m
(which satisfies Stream s m Char
), but GHC doesn’t know this. So GHC makes the assumption that here, uncons i :: m0 (Maybe (t, s))
, where m0
is some arbitrary monad. Thus GHC produces the error you see, since uncons
requires the constraint Stream s m0 Char
to be satisfied in order to use it, yet that constraint isn’t necessarily satisfied for any random m0
.
How can this be solved? Well, you already know that this whole computation is of type ParsecT s (ShiftedState u s) m Char
, and m
satisfies the instance Stream s m Char
. And as it turns out, there is a function lift :: Monad m => m a -> ParsecT s u m a
which ‘lifts’ a monadic computation m a
to a Parsec computation ParsecT s u m a
. So you can just rewrite your function to:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Parsect
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