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 -- Parser -- Re-export Happy parser functionalities
13 module Parser
14 ( parse,
15 Program (..),
16 ImportDecl (..),
17 FieldDecl (..),
18 MethodDecl (..),
19 FieldElem (..),
20 Type (..),
21 Argument (..),
22 Block (..),
23 Statement (..),
24 Location (..),
25 AssignExpr (..),
26 MethodCall (..),
27 ImportArg (..),
28 CounterUpdate (..),
29 Expr (..),
30 )
31 where
32
33 import Data.ByteString.Lazy (ByteString)
34 import Lexer (Alex (..), Token, runAlex, getAlexState, AlexUserState(..), AlexState(..))
35 import Parser.Grammar
36 import Parser.Tree
37 import Util.SourceLoc as SL
38 import Data.Text qualified as Text
39 import Types
40
41 parse :: ByteString -> Either [CompileError] Program
42 parse input = case runAlex input parseAndHandleError of
43 Left m -> Left [CompileError Nothing $ Text.pack m]
44 Right (errs, _) | not $ null errs -> Left errs
45 Right (_, program) -> Right program
46 where
47 parseAndHandleError = parseInternal >>= \program -> do
48 state <- getAlexState
49 let AlexUserState{errors = errors} = alex_ust state
50 return (errors, program)