packages feed

rtk-0.11: test/golden/debug-test/DebugTestLexer.x

-- Generated by RTK from grammar 'DebugTest'. Do not edit by hand.
{
{-# LANGUAGE StandaloneDeriving, DeriveDataTypeable #-}
module DebugTestLexer(scanTokens, alexScanTokens, Token(..), PosToken(..), AlexPosn(..))
where
import Data.Data (Data)

 }
%wrapper "monad"


tokens :- "}" { simple Tk__tok__symbol__12 }
          "{" { simple Tk__tok__symbol__11 }
          "while" { simple Tk__tok_while_10 }
          "unused" { simple Tk__tok_unused_13 }
          "if" { simple Tk__tok_if_8 }
          "else" { simple Tk__tok_else_9 }
          "=" { simple Tk__tok__eql__0 }
          ";" { simple Tk__tok__semi__1 }
          "/" { simple Tk__tok__symbol__5 }
          "-" { simple Tk__tok__minus__3 }
          "+" { simple Tk__tok__plus__2 }
          "*" { simple Tk__tok__star__4 }
          ")" { simple Tk__tok__rparen__7 }
          "(" { simple Tk__tok__lparen__6 }
          (['0'-'9']+) { simple1 $  Tk__number . (id) }
          (['a'-'z'\ 'A'-'Z']  ['a'-'z'\ 'A'-'Z'\ '0'-'9']*) { simple1 $  Tk__identifier . (id) }
          ("$"  "UnusedRule2"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_UnusedRule2 . ((tail . dropWhile (/= ':'))) }
          ("$"  "UnusedRule1"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_UnusedRule1 . ((tail . dropWhile (/= ':'))) }
          ("$"  "Block"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_Block . ((tail . dropWhile (/= ':'))) }
          ("$"  "WhileLoop"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_WhileLoop . ((tail . dropWhile (/= ':'))) }
          ("$"  "IfStatement"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_IfStatement . ((tail . dropWhile (/= ':'))) }
          ("$"  "Factor"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_Factor . ((tail . dropWhile (/= ':'))) }
          ("$"  "Term"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_Term . ((tail . dropWhile (/= ':'))) }
          ("$"  "Expression"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_Expression . ((tail . dropWhile (/= ':'))) }
          ("$"  "Assignment"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_Assignment . ((tail . dropWhile (/= ':'))) }
          ("$"  "Statement"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_Statement . ((tail . dropWhile (/= ':'))) }
          ("$"  "Program"  ":"  [a-zA-Z_]  [A-Za-z0-9_]*) { simple1 $  Tk__qq_Program . ((tail . dropWhile (/= ':'))) }
          . { rtkError }

{
data Token = EndOfFile |
             Tk__tok__symbol__12 |
             Tk__tok__symbol__11 |
             Tk__tok_while_10 |
             Tk__tok_unused_13 |
             Tk__tok_if_8 |
             Tk__tok_else_9 |
             Tk__tok__eql__0 |
             Tk__tok__semi__1 |
             Tk__tok__symbol__5 |
             Tk__tok__minus__3 |
             Tk__tok__plus__2 |
             Tk__tok__star__4 |
             Tk__tok__rparen__7 |
             Tk__tok__lparen__6 |
             Tk__number String |
             Tk__identifier String |
             Tk__qq_UnusedRule2 String |
             Tk__qq_UnusedRule1 String |
             Tk__qq_Block String |
             Tk__qq_WhileLoop String |
             Tk__qq_IfStatement String |
             Tk__qq_Factor String |
             Tk__qq_Term String |
             Tk__qq_Expression String |
             Tk__qq_Assignment String |
             Tk__qq_Statement String |
             Tk__qq_Program 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)

}