Question

Consider the following, working, Alex source file:

{
module Main (main) where
}

%wrapper "basic"

tokens :-
    $white ;
    .      { rule "!"}

{

type Token = String

rule tok = \s -> tok

main = do
    s <- getContents
    mapM_ print (alexScanTokens s)
}

I would love to put my helper code closer to the top of the file, before all the rules. I tried doing this:

{
module Main (main) where
}

%wrapper "basic"

{
type Token = String

rule tok = \s -> tok
}

tokens :-
    $white ;
    .      { rule "!"}

{

main = do
    s <- getContents
    mapM_ print (alexScanTokens s)
}

but got the following error:

test.x:11:2: parse error

(line 11 is the closing curly brace after my helper code)

Is there a way to move my helper code closer to the top of the file?

I also tried putting the helper code in the first block, together with "module Main" declaration but that didn't work because the "%wrapper" bit generates some import statements that need to appear right as the first thing in the generated file.

Was it helpful?

Solution

Quoting from the documentation of Alex:

"The overall layout of an Alex file is:

alex := [ @code ] [ wrapper ] { macrodef } @id ':-' { rule } [ @code ]

At the top of the file, the code fragment is normally used to declare the module name and some imports, and that is all it should do: don't declare any functions or types in the top code fragment, because Alex may need to inject some imports of its own into the generated lexer code, and it does this by adding them directly after this code fragment in the output file."

So, what you are trying to do violates the syntax. It seems that the sole place you can put the definition of the Token datatype is at the final code block.

However, it is possible to have this code in a separate module, if you like, and import it at the top code block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top