never executed always true always false
    1 -- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
    2 --
    3 -- This file is a part of decafc.
    4 --
    5 -- decafc is free software: you can redistribute it and/or modify it under the
    6 -- terms of the MIT (X11) License as described in the LICENSE file.
    7 --
    8 -- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
    9 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   10 -- FOR A PARTICULAR PURPOSE.  See the X11 license for more details.
   11 
   12 module Parser.Helper where
   13 
   14 import Data.Text (Text)
   15 import Lexer (Alex (..), Token (..), alexMonadScan, alexError, addError)
   16 import Util.SourceLoc as SL
   17 import Text.Printf (printf)
   18 import Types (CompileError(CompileError))
   19 import Formatting (sformat, shown, (%))
   20 
   21 getID :: Token -> Text
   22 getID (Identifier id) = id
   23 
   24 getLiteral :: Token -> Text
   25 getLiteral (IntLiteral i) = i
   26 getLiteral (BooleanLiteral b) = b
   27 getLiteral (CharLiteral c) = c
   28 getLiteral (StringLiteral s) = s
   29 
   30 getOp :: Token -> Text
   31 getOp (IncrementOp op) = op
   32 getOp (CompoundAssignOp op) = op
   33 
   34 unionOf :: SL.Located a -> SL.Located b -> Range
   35 unionOf (SL.LocatedAt loc1 _) (SL.LocatedAt loc2 _) = combineRanges loc1 loc2
   36   where
   37     combineRanges (SL.Range start1 stop1) (SL.Range start2 stop2) =
   38       let start = if (SL.offset start1) < (SL.offset start2) then start1 else start2
   39           stop = if (SL.offset stop1) > (SL.offset stop2) then stop1 else stop2
   40        in SL.Range start stop
   41 
   42 lexerwrap :: (SL.Located Token -> Alex a) -> Alex a
   43 lexerwrap s = do
   44   token <- alexMonadScan
   45   s token
   46 
   47 parseError (SL.LocatedAt sl@(SL.Range (SL.Posn _ row col) _) tok) = do
   48   addError $ CompileError (Just sl) $ sformat ("Error handling token '" % shown % "'") tok
   49   alexError "Parser failed."