rtk-0.12: test/golden/grammar/GrammarLexer.x
-- Generated by RTK from grammar 'Grammar'. Do not edit by hand.
{
{-# LANGUAGE StandaloneDeriving, DeriveDataTypeable #-}
module GrammarLexer(scanTokens, alexScanTokens, Token(..), PosToken(..), AlexPosn(..))
where
import Data.Data (Data)
import Data.List
import qualified Data.Map as M
}
%wrapper "monad"
$dq = "
$ndq = [^\"]
$backslash = [\\]
tokens :- "tok_Clause_dummy_10" { simple Tk__tok_Clause_dummy_10 }
"tok_Grammar_dummy_11" { simple Tk__tok_Grammar_dummy_11 }
"tok_IdList_dummy_9" { simple Tk__tok_IdList_dummy_9 }
"tok_Name_dummy_8" { simple Tk__tok_Name_dummy_8 }
"tok_Option_dummy_7" { simple Tk__tok_Option_dummy_7 }
"tok_OptionList_dummy_6" { simple Tk__tok_OptionList_dummy_6 }
"tok_Rule_dummy_5" { simple Tk__tok_Rule_dummy_5 }
"tok_RuleList_dummy_4" { simple Tk__tok_RuleList_dummy_4 }
"tok_StrLit_dummy_3" { simple Tk__tok_StrLit_dummy_3 }
"~" { simple Tk__tok__tilde__14 }
"|" { simple Tk__tok__pipe__11 }
"imports" { simple Tk__tok_imports_2 }
"grammar" { simple Tk__tok_grammar_0 }
"@symmacro" { simple Tk__tok__symbol_symmacro_9 }
"@shortcuts" { simple Tk__tok__symbol_shortcuts_6 }
"?" { simple Tk__tok__symbol__16 }
"=" { simple Tk__tok__eql__3 }
";" { simple Tk__tok__semi__1 }
":" { simple Tk__tok__colon__4 }
"." { simple Tk__tok__dot__5 }
"," { simple Tk__tok__coma__10 }
"+" { simple Tk__tok__plus__15 }
"*" { simple Tk__tok__star__13 }
")" { simple Tk__tok__rparen__8 }
"(" { simple Tk__tok__lparen__7 }
"!" { simple Tk__tok__exclamation__12 }
("/*" ([^\*]| [\*] [^\/]| [\n])* "*/") ;
("#" .* [\n]) ;
([\ \t\n]+) ;
("[" ([^\]]| "\]")* "]") { simple1 $ Tk__regexplit . (id) }
($dq $dq $dq ($ndq| $dq $ndq| $dq $dq $ndq| [\n])* $dq $dq $dq) { simple1 $ Tk__bigstr . (id) }
("'" ([^\\']| $backslash .)* "'") { simple1 $ Tk__str . (id) }
([a-zA-Z] [A-Za-z0-9_]*) { simple1 $ Tk__id . (id) }
("$" "Name" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_Name . ((tail . dropWhile (/= ':'))) }
("$" "StrLit" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_StrLit . ((tail . dropWhile (/= ':'))) }
("$" "Clause" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_Clause . ((tail . dropWhile (/= ':'))) }
("$" "IdList" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_IdList . ((tail . dropWhile (/= ':'))) }
("$" "Option" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_Option . ((tail . dropWhile (/= ':'))) }
("$" "OptionList" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_OptionList . ((tail . dropWhile (/= ':'))) }
("$" "Rule" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_Rule . ((tail . dropWhile (/= ':'))) }
("$" "RuleList" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_RuleList . ((tail . dropWhile (/= ':'))) }
("$" "Grammar" ":" [a-zA-Z_] [A-Za-z0-9_]*) { simple1 $ Tk__qq_Grammar . ((tail . dropWhile (/= ':'))) }
. { rtkError }
{
data Token = EndOfFile |
Tk__tok_Clause_dummy_10 |
Tk__tok_Grammar_dummy_11 |
Tk__tok_IdList_dummy_9 |
Tk__tok_Name_dummy_8 |
Tk__tok_Option_dummy_7 |
Tk__tok_OptionList_dummy_6 |
Tk__tok_Rule_dummy_5 |
Tk__tok_RuleList_dummy_4 |
Tk__tok_StrLit_dummy_3 |
Tk__tok__tilde__14 |
Tk__tok__pipe__11 |
Tk__tok_imports_2 |
Tk__tok_grammar_0 |
Tk__tok__symbol_symmacro_9 |
Tk__tok__symbol_shortcuts_6 |
Tk__tok__symbol__16 |
Tk__tok__eql__3 |
Tk__tok__semi__1 |
Tk__tok__colon__4 |
Tk__tok__dot__5 |
Tk__tok__coma__10 |
Tk__tok__plus__15 |
Tk__tok__star__13 |
Tk__tok__rparen__8 |
Tk__tok__lparen__7 |
Tk__tok__exclamation__12 |
Tk__regexplit String |
Tk__bigstr String |
Tk__str String |
Tk__id String |
Tk__qq_Name String |
Tk__qq_StrLit String |
Tk__qq_Clause String |
Tk__qq_IdList String |
Tk__qq_Option String |
Tk__qq_OptionList String |
Tk__qq_Rule String |
Tk__qq_RuleList String |
Tk__qq_Grammar String
deriving (Show)
-- AlexPosn is defined by the alex wrapper, so the Data instance (required by
-- the parser's RtkPos position type) can only be attached via standalone
-- deriving - the same solution as the hand-written Lexer.x
deriving instance Data AlexPosn
-- A token together with the source position where it starts
data PosToken = PosToken { ptPos :: AlexPosn, ptToken :: Token }
deriving (Show)
alexEOF = do
(pos, _, _, _) <- alexGetInput
return $ PosToken pos EndOfFile
-- Lex the input into a token stream, returning the positioned error message
-- on a lexical error (encoded as "LINE:COL:message", see rtkError below).
-- The returned list always ends with an EndOfFile token that carries the
-- position of the end of input, so parse errors at end of input can be
-- reported with a position too
scanTokens :: String -> Either String [PosToken]
scanTokens str = runAlex str $ do
let loop toks = do tok <- alexMonadScan
case tok of
PosToken _ EndOfFile -> return $ reverse (tok : toks)
_ -> let toks' = tok : toks
in toks' `seq` loop toks'
loop []
-- Thin compatibility wrapper: callers that have not switched to 'scanTokens'
-- get the error message thrown instead
alexScanTokens :: String -> [PosToken]
alexScanTokens str =
case scanTokens str of
Right toks -> toks
Left err -> errorWithoutStackTrace err
simple1 :: (String -> Token) -> AlexInput -> Int -> Alex PosToken
simple1 t (pos, _, _, str) len = return $ PosToken pos (t (take len str))
simple :: Token -> AlexInput -> Int -> Alex PosToken
simple t (pos, _, _, _) len = return $ PosToken pos t
-- Encode the position as "LINE:COL:message" so callers can split it back out
-- into a structured position - the same encoding the rtk grammar lexer uses
rtkError ((AlexPn _ line column), _, _, str) len = alexError $ (show line) ++ ":" ++ (show column) ++ ":lexical error. Following chars: " ++ (take 10 str)
}