Rohit Garg
Rohit Garg

Reputation: 31

How to match newlines with Alex/Haskell

I borrowed the example presented here

http://www.haskell.org/alex/doc/html/introduction.html

I am trying to make an interpreter for numerical expressions. (literals only, no variables) And I want to have newlines separate two different expressions. Unfortunaltely, when I do this

$ignoredWhite = [\t\f\v\r] -- ignored whitespace

$newline = "\n" --new line

Alex (v 2.2) aborts compilation. I have tried

$newline = \n --new line

but that just causes the abort to move from alex to final binary (aka one generated by ghc), when it is reading the input.

How do I go about fixing this?

Upvotes: 3

Views: 968

Answers (2)

KCH
KCH

Reputation: 2854

This question is really old, nevertheless I'll post my answer for future visitors. When the tokens are defined, I tell alex to process all whitespace:

tokens :-
  $white {\p s -> checkWhite p $ head s} 

checkWhite looks like this:

checkWhite :: AlexPosn -> Char -> Token
checkWhite p '\n' = TNewLine p
checkWhite p _ = TEmpty

As you can see, it outputs a special token when newline is encountered. To delete TEmpty from output, you can use ... filter (\= TEmpty) $ alexScanTokens ....

Upvotes: 3

Gian
Gian

Reputation: 13955

Just a guess - have you tried including it in a character range?

$newline = [\n]

Upvotes: 2

Related Questions