How to specify tab width for Alex lexer?

Alex documentation (Chapter 5) says:

You might want Alex to keep track of the line and column number in the input text, or you might wish to do it yourself (perhaps you use a different tab width from the standard 8-columns, for example)

But changing tab width from 8 to 4 in Alex position tracker is rather hard than easy. The code for this is hidden deep inside Alex generated routines:

-- this function is used by `alexGetByte`, which is used by `alex_scan_tkn`, which is 
-- used by `alexScanUser` and `alexRightContext`,
-- which is used by `alex_accept` etc etc...
alexMove :: AlexPosn -> Char -> AlexPosn
alexMove (AlexPn a l c) 't' = AlexPn (a+1)  l     (((c+7) `div` 8)*8+1)
alexMove (AlexPn a l c) 'n' = AlexPn (a+1) (l+1)   1
alexMove (AlexPn a l c) _    = AlexPn (a+1)  l     (c+1)

One idea is to create your own wrapper which defines alexMove the way you want it.

On my Mac wrappers are installed in /Library/Haskell/ghc-7.6.3/lib/alex-3.0.5/share/

Look for where files named "AlexWrapper-monad", "AlexWrapper-monad-bytestring", ... reside on your system.

The "-t" command line option tells alex where to look for templates, but it also might pertain to wrappers since it appears that wrappers and templates reside in the same directory.

链接地址: http://www.djcxy.com/p/65634.html

上一篇: Happy和Alex如何引导自己成为自己?

下一篇: 如何为Alex lexer指定制表符宽度?