fparsec

Parsing the arrow type with FParsec

孤人 提交于 2019-12-11 16:58:01
问题 I'm trying to parse the arrow type with FParsec. That is, this: Int -> Int -> Int -> Float -> Char For example. I tried with this code, but it only works for one type of arrow ( Int -> Int ) and no more. I also want to avoid parentheses, because I already have a tuple type that uses them, and I don't want it to be too heavy in terms of syntax either. let ws = pspaces >>. many pspaces |>> (fun _ -> ()) let str_ws s = pstring s .>> ws type Type = ArrowType of Type * Type let arrowtype' = pipe2

Differentiating logical from other infix operators

家住魔仙堡 提交于 2019-12-11 02:44:14
问题 I'm trying to parse SQL search conditions and having trouble getting the parser to differentiate logical ( AND , OR ) from other infix operators. I'm parsing them as different nodes (perhaps that's difficult to do), but simplifies the evaluation phase. Here's the relevant code snippet (I can include more if necessary). let opp = OperatorPrecedenceParser<_,_,_>() let scalarExpr = opp.ExpressionParser opp.TermParser <- constant <|> id <|> between lparen rparen scalarExpr <|> scalarExpr //infix

Indentation, expressions, statements and StackOverflowException with FParsec - Errors

与世无争的帅哥 提交于 2019-12-11 02:36:16
问题 I test indentation with FParsec, according to this implementation, but when I make it a little more complex by adding expressions (literals, lists, tuples and arithmetic operations), allowing expressions to top-level, and adding a variable creation statement; I first get a StackOverflowException error . In my opinion, this is because the expression parser is solicited in such a way as to make an infinite loop in the program. I see no other reason, however, I don't know how to fix this problem

FParsec only parses expr between parentheses

不想你离开。 提交于 2019-12-11 02:08:29
问题 I am coding a parser (for learning pourpuses). I want it to parse constructions like let myVar be 40 plus 2 and let myVar be (40 plus 2) With no problems... but my parser does not "understand" the former. It sees the 40 and thinks "well, it's a Literal Numeric 40 ". When I put parentheses, my parser works great. I am having a hard time to understand why. Parser: type value = | Boolean of bool | Numeric of float | String of string type arithmetic = Sum | Sub | Mul | Div | Pow type logic = And

fparsec to parse sequence of string

随声附和 提交于 2019-12-10 21:45:18
问题 I have an user input text like "abc,def,ghi". I want to parse it to get list of string as ["abc", "def"]. I tried let str : Parser<_> = many1Chars (noneOf ",") let listParser : Parser<_> = many (str);; but it always give me the first item only ["abc"]. "Def" and others are not coming in result list 回答1: You're parsing up to the first comma, but not parsing the comma itself. To parse a list of things separated by other things, use sepBy: let comma = pstring "," let listParser = sepBy str comma

Improving the readability of a FParsec parser

六月ゝ 毕业季﹏ 提交于 2019-12-10 18:47:20
问题 I have a hand-written CSS parser done in C# which is getting unmanageable and was trying to do it i FParsec to make it more mantainable. Here's a snippet that parses a css selector element made with regexes: var tagRegex = @"(?<Tag>(?:[a-zA-Z][_\-0-9a-zA-Z]*|\*))"; var idRegex = @"(?:#(?<Id>[a-zA-Z][_\-0-9a-zA-Z]*))"; var classesRegex = @"(?<Classes>(?:\.[a-zA-Z][_\-0-9a-zA-Z]*)+)"; var pseudoClassRegex = @"(?::(?<PseudoClass>link|visited|hover|active|before|after|first-line|first-letter))";

Parsing function application with FParsec using OperatorPrecedenceParser?

谁都会走 提交于 2019-12-10 04:19:22
问题 The question is similar to this one, but I want to parse an expression with function application using the OperatorPrecedenceParser in FParsec . Here is my AST: type Expression = | Float of float | Variable of VarIdentifier | BinaryOperation of Operator * Expression * Expression | FunctionCall of VarIdentifier (*fun name*) * Expression list (*arguments*) I have the following input: board→create_obstacle(4, 4, 450, 0, fric) And here is the parser code: let expr = (number |>> Float) <|> (ident

FParsec and a delimiter based syntax

风流意气都作罢 提交于 2019-12-08 05:44:05
问题 I'm trying to use fparsec to parse a simple todo list language (the data from TaskPaper actually) as a simple parser combinator example. But I've run into a bug I can't seem to puzzle out. I'm new to parser combinators and FParsec seems to rely on me knowing Parsec, but I'm finding the parsec documentation inscrutable. The rules of the task paper language are simple (I'm ignoring @tags for now) Projects end with a ':' Tasks are start with '-' Any other line of text is a plain text note on

Parsing int or float with FParsec

大兔子大兔子 提交于 2019-12-06 02:09:09
问题 I'm trying to parse a file, using FParsec, which consists of either float or int values. I'm facing two problems that I can't find a good solution for. 1 Both pint32 and pfloat will successfully parse the same string, but give different answers, e.g pint32 will return 3 when parsing the string "3.0" and pfloat will return 3.0 when parsing the same string. Is it possible to try parsing a floating point value using pint32 and have it fail if the string is "3.0" ? In other words, is there a way

How do I test for exactly 2 characters with fparsec?

假装没事ソ 提交于 2019-12-05 16:12:11
I have the following program that runs. It takes a line of text and splits it into two parts, the first is an identifier and the second is the remainder of the line. My parser for the identifier (factID) takes any string of characters as the identifier, which is not (quite) what I want. What I want is a parser that only succeeds when it encounters two consecutive upper case letters. So for example "AA" should succeed while "A", "A1" or "AAA" should not. What I can't figure out is how construct a parser that looks for a fixed length token. I thought perhaps CharParsers.next2CharsSatisfy might