Hi I am trying to parse a list of elements separated by the word and and in between curly brackets. the curly brackets and the "and"s could be surrounded by spaces or newlines. I wrote this parser:
testBodyP :: Parser [Tester.Condition]
testBodyP =
Parsec.between
(Parsec.char '{' <* Parsec.spaces)
(Parsec.spaces *> Parsec.char '}')
(Parsec.sepBy
condtionP
(Parsec.spaces *> Parsec.string "and" <* Parsec.spaces))
but the parser fails with this error:
unexpected "}"
expecting space or "and"
on this script:
("breaks", "echo -n does this test work") should {
contain("byeeeee") and
contain("nope")
}
but it does work on this script
("breaks", "echo -n does this test work") should {
contain("byeeeee") and
contain("nope")}
How can I fix this?
Thanks!
edit (response to Noughtmare):
it just calls other parses I defined so it would be ugly to put it here. If you want checkout the source code on my github: https://github.com/RHL120/sbt
edit:
I changed the testBodyP function on the github repo to and used spaces as a seprator. Now it "works"
Related
For example, if I were to do this:
print(“\”)
It would say: `unfinished string near: ‘“”’
instead of my expected output of: ‘\’
How would I print this? I have searched on google yet still have yet to find an answer.
The backslash (\) is escaping the following character, being the double quote ("), causing the string to be unfinished.
To include an actual backslash in your string, you escape it with another backslash:
print("\\")
From Lua 5.4 Reference Manual, §3.1 (emphasis mine):
A short literal string can be delimited by matching single or double quotes, and can contain the following C-like escape sequences: '\a' (bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\v' (vertical tab), '\\' (backslash), '"' (quotation mark [double quote]), and ''' (apostrophe [single quote]). [...]
As as exercise of the Haskell Book written by bitemyapp I need to make a parser which can parse the given log.
So I made this : https://gist.github.com/RoelofWobben/79058b1a6a5c24f08a495045c7a685f9
but when I test it with : ` parseString parseMultipleDays myLog I see this error message :
Failure (ErrInfo {_errDoc = (interactive):3:1: error: expected: new-line
# 2025-02-05
^ , _errDeltas = [Lines 2 0 20 0]})
anyone who can give me a hint where the bug is. When needed I can make a repo of the code I have with some tests.
I use trifecta because that one is explained in the chapter
string "--" *> manyTill anyChar newline *> newline
manyTill already consumes the terminator, so the above defines a comment to be "--", followed by anything, followed by two newlines.
Your input only contains one newline after the comment, so you get an error telling you that the parser expected a second newline instead of the #.
I am creating a file that will be compiled with flex, but I am having trouble understanding why I am getting this error. I am inexperienced with this.
The error says line 43 (ie the last line) end of file inside the action.
What I have so far.
%{
#ifdef PRINT
#define TOKEN(t) printf("Token: " #t "/n");
#else
#define TOKEN(t) return(t);
#endif
%}
%%
"," TOKEN(COMMA")
";" TOKEN(SEMICOLON)
"->" TOKEN(ARROW)
"(" TOKEN(BRA)
")" TOKEN(KET)
"=" TOKEN(EQUALS)
"<>" TOKEN(LESMORE)
"<" TOKEN(LESS)_THAN)
">" TOKEN(MORE_THAN)
"<=" TOKEN(LESS_EQUAL)
">=" TOKEN(MORE_EQUAL)
"*" TOKEN(MULTIPLY)
"/" TOKEN(DIVIDE)
"'" TOKEN(CHAR_SHOW)
ENDP TOKEN(ENDP)
DECLARATIONS TOKEN(DECLARATIONS)
CHARACTER TOKEN(CHARACTER)
INTEGER TOKEN(INTEGER)
REAL TOKEN(REAL)
ENDIF TOKEN(ENDIF)
ELSE TOKEN(ELSE)
ENDDO TOKEN(ENDDO)
WHILE TOKEN(WHILE)
DO TOKEN(DO)
ENDWHILE TOKEN(ENDWHILE)
FOR TOKEN(FOR)
IS TOKEN(IS)
BY TOKEN(BY)
TO TOKEN(TO)
ENDFOR TOKEN(ENDFOR)
WRITE TOKEN(WRITE)
NEWLINE TOKEN(NEWLINE)
READ TOKEN(READ)
%%
Any help is appreciated
The first action is:
"," TOKEN(COMMA")
which has a mismatched quote.
Also, there is a problem with
"<" TOKEN(LESS)_THAN)
And it is not clear to me if all the lines from that one down are incorrectly indented by one space; if so, that is also a problem.
Finally, there is very little point in that TOKEN macro (which is probably copied from somewhere else where it is unnecessary) because you can use the --debug command-line option to Flex to produce very accurate scanner traces, and there is a similar tracing facility in bison which will also reveal the result of the scanner (including the name of the token, which the flex trace does not, unfortunately, provide).
You have two Typos, you have to change that to :
Line 10 : "," TOKEN(COMMA") --> "," TOKEN(COMMA)
Line 17: "<" TOKEN(LESS)_THAN) --> "<" TOKEN(LESS_THAN)
I try to parse this piece of text
:20: test :254:
aapje
:21: rest
...
:20: and :21: are special tags, because they start the line. :254: should be 'normal' text, as it does not start on a newline.
I would like the result to be
(20, 'test :254: \naapje')
(21, 'rest')
Lines are terminated using either \r\n or '\n'
I started out trying to ignore the whitespace, but then I match the ':254:' tag as well. So I have to create something that uses the whitespace information.
What I would like to be able to do is something like this:
lexer grammar MT9740_lexer;
InTagNewLine : '\r\n' ~':';
ReadNewLine :'\r\n' ;
But the first would consume the : How can I still generate these tokens? Or is there a smarted approach?
What I understand is that you're looking for some lexer rules that match the start of a line. This lexer rule should tokenize your :20: or :21: appearing at the start of a line only
SOL : {getCharPositionInLine() == 0}? ':' [0-9]+ ':' ;
Your parser rules can then look for this SOL token before parsing the rest of the line.
I am playing around with the DemoApp that comes bundled with ParseKit and I am having a strange issue.
I create this grammar:
#start = tag; tag = '{' Word ';' Word '}';
and it parses a sentence like:
{foo;bar}
just fine. The issue arises when I change the grammar slightly to be:
#start = tag; tag = '{' Word '\' Word '}';
This causes the app the crash. ParseKit does not seem to like the \ token.
Any ideas?
Developer of ParseKit here.
Great example! You've found a bug in ParseKit related to escaped backslashes inside quoted strings. I've fixed the issue. Please update to HEAD of trunk from Google Code.
However, this is also an issue in your example grammar. You will need to escape the backslash in your quoted string. Otherwise it will be escaping the "close quote". So change to this:
#start = tag; tag = '{' Word '\\' Word '}';