|
@ -37,6 +37,13 @@ import Control.Monad |
|
|
|
|
|
|
|
|
QVAR { Token (TokQual VarId _ _) _ _ } |
|
|
QVAR { Token (TokQual VarId _ _) _ _ } |
|
|
QCON { Token (TokQual ConId _ _) _ _ } |
|
|
QCON { Token (TokQual ConId _ _) _ _ } |
|
|
|
|
|
|
|
|
|
|
|
VARSYM { Token (TokUnqualOp VarId _) _ _ } |
|
|
|
|
|
CONSYM { Token (TokUnqualOp ConId _) _ _ } |
|
|
|
|
|
|
|
|
|
|
|
QVARSYM { Token (TokQualOp VarId _ _) _ _ } |
|
|
|
|
|
QCONSYM { Token (TokQualOp ConId _ _) _ _ } |
|
|
|
|
|
|
|
|
STRING { Token (TokString _) _ _ } |
|
|
STRING { Token (TokString _) _ _ } |
|
|
'eof' { Token TokEof _ _ } |
|
|
'eof' { Token TokEof _ _ } |
|
|
|
|
|
|
|
@ -63,11 +70,13 @@ import Control.Monad |
|
|
';' { Token TokSemi _ _ } |
|
|
';' { Token TokSemi _ _ } |
|
|
'=' { Token TokEqual _ _ } |
|
|
'=' { Token TokEqual _ _ } |
|
|
',' { Token TokComma _ _ } |
|
|
',' { Token TokComma _ _ } |
|
|
|
|
|
'`' { Token TokTick _ _ } |
|
|
|
|
|
|
|
|
'let' { Token TokLet _ _ } |
|
|
'let' { Token TokLet _ _ } |
|
|
'in' { Token TokIn _ _ } |
|
|
'in' { Token TokIn _ _ } |
|
|
'data' { Token TokData _ _ } |
|
|
'data' { Token TokData _ _ } |
|
|
'case' { Token TokCase _ _ } |
|
|
'case' { Token TokCase _ _ } |
|
|
|
|
|
'of' { Token TokOf _ _ } |
|
|
'module' { Token TokModule _ _ } |
|
|
'module' { Token TokModule _ _ } |
|
|
'where' { Token TokWhere _ _ } |
|
|
'where' { Token TokWhere _ _ } |
|
|
|
|
|
|
|
@ -82,8 +91,8 @@ Exp :: { Exp } |
|
|
| InfixExp { $1 } |
|
|
| InfixExp { $1 } |
|
|
|
|
|
|
|
|
InfixExp :: { Exp } |
|
|
InfixExp :: { Exp } |
|
|
: LeftExp {- ... -} { $1 } |
|
|
|
|
|
-- | LeftExp qop InfixExp { Infix $1 (getVar $2) $3 } |
|
|
|
|
|
|
|
|
: LeftExp {- ... -} { $1 } |
|
|
|
|
|
| LeftExp qop InfixExp { span $1 $3 $ Infix $1 $2 $3 } |
|
|
|
|
|
|
|
|
LeftExp :: { Exp } |
|
|
LeftExp :: { Exp } |
|
|
: '\\' Apat List(Apat) '->' Exp { span $1 $5 (makeLams ($2:$3) $5) } |
|
|
: '\\' Apat List(Apat) '->' Exp { span $1 $5 (makeLams ($2:$3) $5) } |
|
@ -95,10 +104,10 @@ FuncExp :: { Exp } |
|
|
| Aexp { $1 } |
|
|
| Aexp { $1 } |
|
|
|
|
|
|
|
|
Aexp :: { Exp } |
|
|
Aexp :: { Exp } |
|
|
: qvar { span $1 $1 $ Ref $1 } |
|
|
|
|
|
| qcon { span $1 $1 $ Con $1 } |
|
|
|
|
|
| '(' CommaList(Exp) ')' { span $1 $3 $ makeTuple $2 } |
|
|
|
|
|
| STRING { span $1 $1 $ Literal (LitString (getString $1)) } |
|
|
|
|
|
|
|
|
: qvar { span $1 $1 $ Ref $1 } |
|
|
|
|
|
| gcon { span $1 $1 $ Con $1 } |
|
|
|
|
|
| '(' CommaList1(Exp) ')' { span $1 $3 $ makeTuple $2 } |
|
|
|
|
|
| STRING { span $1 $1 $ Literal (LitString (getString $1)) } |
|
|
|
|
|
|
|
|
Type :: { Type } |
|
|
Type :: { Type } |
|
|
: Btype '->' Type { span $1 $3 $ Tyarr $1 $3 } |
|
|
: Btype '->' Type { span $1 $3 $ Tyarr $1 $3 } |
|
@ -109,20 +118,24 @@ Btype :: { Type } |
|
|
| Atype { $1 } |
|
|
| Atype { $1 } |
|
|
|
|
|
|
|
|
Atype :: { Type } |
|
|
Atype :: { Type } |
|
|
: qvar { span $1 $1 $ Tyvar $1 } |
|
|
|
|
|
| qcon { span $1 $1 $ Tycon $1 } |
|
|
|
|
|
| '(' CommaList(Type) ')' { span $1 $3 $ makeTupleType $2 } |
|
|
|
|
|
|
|
|
: qvar { span $1 $1 $ Tyvar $1 } |
|
|
|
|
|
| gtycon { span $1 $1 $ Tycon $1 } |
|
|
|
|
|
| '[' Type ']' { span $1 $3 $ Tylist $2 } |
|
|
|
|
|
| '(' CommaList1(Type) ')' { span $1 $3 $ makeTupleType $2 } |
|
|
|
|
|
|
|
|
Pat :: { Pat } |
|
|
Pat :: { Pat } |
|
|
: Lpat { $1 } |
|
|
: Lpat { $1 } |
|
|
|
|
|
|
|
|
Lpat :: { Pat } |
|
|
Lpat :: { Pat } |
|
|
: Apat { $1 } |
|
|
|
|
|
|
|
|
: Apat { $1 } |
|
|
|
|
|
| gcon Apat List(Apat) { span $1 (endOfListPos $2 $3) (ConPat $1 ($2 : $3)) } |
|
|
|
|
|
| Lpat qconop Pat { span $1 $3 $ InfixPat $1 $2 $3 } |
|
|
|
|
|
|
|
|
Apat :: { Pat } |
|
|
Apat :: { Pat } |
|
|
: VAR { span $1 $1 $ Var (getVar $1) } |
|
|
: VAR { span $1 $1 $ Var (getVar $1) } |
|
|
|
|
|
| gcon { span $1 $1 $ ConPat $1 [] } |
|
|
| '_' { span $1 $1 $ Wildcard } |
|
|
| '_' { span $1 $1 $ Wildcard } |
|
|
| '(' CommaList(Pat) ')' { span $1 $3 $ makeTuplePattern $2 } |
|
|
|
|
|
|
|
|
| '(' CommaList1(Pat) ')' { span $1 $3 $ makeTuplePattern $2 } |
|
|
|
|
|
|
|
|
Decl :: { Decl } |
|
|
Decl :: { Decl } |
|
|
: CommaList1(VAR) '::' Type { TySig (getVar `fmap` $1) $3 (startPosn (head $1)) (endPosn $3) } |
|
|
: CommaList1(VAR) '::' Type { TySig (getVar `fmap` $1) $3 (startPosn (head $1)) (endPosn $3) } |
|
@ -186,15 +199,72 @@ Semis |
|
|
: ';' Semis { () } |
|
|
: ';' Semis { () } |
|
|
| ';' { () } |
|
|
| ';' { () } |
|
|
|
|
|
|
|
|
-- TODO: qualified names |
|
|
|
|
|
|
|
|
qvarid :: { ParsedVar } |
|
|
|
|
|
qvarid |
|
|
|
|
|
: VAR { getVar $1 } |
|
|
|
|
|
| QVAR { getVar $1 } |
|
|
|
|
|
|
|
|
qvar :: { ParsedVar } |
|
|
qvar :: { ParsedVar } |
|
|
qvar : VAR { getVar $1 } |
|
|
|
|
|
| QVAR { getVar $1 } |
|
|
|
|
|
|
|
|
: qvarid { $1 } |
|
|
|
|
|
| '(' qvarsym ')' { span $1 $3 $2 } |
|
|
|
|
|
|
|
|
|
|
|
qconid :: { ParsedVar } |
|
|
|
|
|
qconid |
|
|
|
|
|
: CON { getVar $1 } |
|
|
|
|
|
| QCON { getVar $1 } |
|
|
|
|
|
|
|
|
|
|
|
qcon :: { ParsedVar } |
|
|
|
|
|
qcon |
|
|
|
|
|
: qconid { $1 } |
|
|
|
|
|
| '(' qconsym ')' { span $1 $3 $2 } |
|
|
|
|
|
|
|
|
|
|
|
qvarsym :: { ParsedVar } |
|
|
|
|
|
qvarsym : VARSYM { getVar $1 } |
|
|
|
|
|
| QVARSYM { getVar $1 } |
|
|
|
|
|
|
|
|
|
|
|
qconsym :: { ParsedVar } |
|
|
|
|
|
qconsym : CONSYM { getVar $1 } |
|
|
|
|
|
| QCONSYM { getVar $1 } |
|
|
|
|
|
|
|
|
|
|
|
qvarop :: { ParsedVar } |
|
|
|
|
|
: qvarsym { $1 } |
|
|
|
|
|
| '`' qvar '`' { span $1 $3 $2 } |
|
|
|
|
|
|
|
|
|
|
|
qconop :: { ParsedVar } |
|
|
|
|
|
: qconsym { $1 } |
|
|
|
|
|
| '`' qcon '`' { span $1 $3 $2 } |
|
|
|
|
|
|
|
|
|
|
|
qop :: { ParsedVar } |
|
|
|
|
|
: qvarop { $1 } |
|
|
|
|
|
| qconop { $1 } |
|
|
|
|
|
|
|
|
|
|
|
gcon :: { ParsedVar } |
|
|
|
|
|
: qcon { $1 } |
|
|
|
|
|
| '(' Commas ')' { BuiltinId { varId = tupleConSymName $2 |
|
|
|
|
|
, varBuiltin = BuiltinTuple $2 |
|
|
|
|
|
, varBegin = startPosn $1 |
|
|
|
|
|
, varEnd = endPosn $3 } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
| '[' ']' { BuiltinId { varId = T.pack "[]" |
|
|
|
|
|
, varBuiltin = BuiltinNil |
|
|
|
|
|
, varBegin = startPosn $1 |
|
|
|
|
|
, varEnd = endPosn $2 } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
gtycon :: { ParsedVar } |
|
|
|
|
|
: gcon { $1 } |
|
|
|
|
|
| '(' '->' ')' { BuiltinId { varId = T.pack "[]" |
|
|
|
|
|
, varBuiltin = BuiltinArrow |
|
|
|
|
|
, varBegin = startPosn $1 |
|
|
|
|
|
, varEnd = endPosn $2 } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
qcon : CON { getVar $1 } |
|
|
|
|
|
| QCON { getVar $1 } |
|
|
|
|
|
|
|
|
Commas :: { Int } |
|
|
|
|
|
: {- empty -} { 0 } |
|
|
|
|
|
| ',' Commas { (let x = $2 in x `seq` 1 + x) } |
|
|
|
|
|
|
|
|
modid : qcon { toModId $1 } |
|
|
|
|
|
|
|
|
modid : qconid { toModId $1 } |
|
|
|
|
|
|
|
|
List(p) |
|
|
List(p) |
|
|
: {-empty-} { [] } |
|
|
: {-empty-} { [] } |
|
@ -225,13 +295,21 @@ type Item = ModuleItem ParsedVar |
|
|
|
|
|
|
|
|
lexer cont = alexMonadScan >>= cont |
|
|
lexer cont = alexMonadScan >>= cont |
|
|
|
|
|
|
|
|
parseError x = alexError (show x) |
|
|
|
|
|
|
|
|
parseError (token, expected) = do |
|
|
|
|
|
(here, _, _, _) <- alexGetInput |
|
|
|
|
|
alexThrow $ \fn -> ParseError { parseErrorMessage = "expecting one of: " ++ unwords expected |
|
|
|
|
|
, parseErrorInlineDesc = Just ("unexpected " ++ show (tokenClass token)) |
|
|
|
|
|
, parseErrorBegin = startPosn token |
|
|
|
|
|
, parseErrorEnd = here |
|
|
|
|
|
, parseErrorFilename = fn } |
|
|
|
|
|
|
|
|
makeLams xs b = foldr Lam b xs |
|
|
makeLams xs b = foldr Lam b xs |
|
|
|
|
|
|
|
|
getVar :: Token -> ParsedVar |
|
|
getVar :: Token -> ParsedVar |
|
|
getVar tok@(Token (TokQual _ p s) _ _) = QualVar { varId = s, varPrefix = p, varBegin = startPosn tok, varEnd = endPosn tok } |
|
|
|
|
|
getVar tok@(Token (TokUnqual _ s) _ _) = UnqualVar { varId = s, varBegin = startPosn tok, varEnd = endPosn tok } |
|
|
|
|
|
|
|
|
getVar tok@(Token (TokQual _ p s) _ _) = QualVar { varId = s, varPrefix = p, varBegin = startPosn tok, varEnd = endPosn tok } |
|
|
|
|
|
getVar tok@(Token (TokUnqual _ s) _ _) = UnqualVar { varId = s, varBegin = startPosn tok, varEnd = endPosn tok } |
|
|
|
|
|
getVar tok@(Token (TokQualOp _ p s) _ _) = QualVar { varId = s, varPrefix = p, varBegin = startPosn tok, varEnd = endPosn tok } |
|
|
|
|
|
getVar tok@(Token (TokUnqualOp _ s) _ _) = UnqualVar { varId = s, varBegin = startPosn tok, varEnd = endPosn tok } |
|
|
getVar _ = error "getVar non-var" |
|
|
getVar _ = error "getVar non-var" |
|
|
|
|
|
|
|
|
getString (Token (TokString s) _ _) = s |
|
|
getString (Token (TokString s) _ _) = s |
|
@ -245,13 +323,22 @@ emptyLol t = LOL (endPosn t) [] |
|
|
lolCons :: a -> LOL a -> LOL a |
|
|
lolCons :: a -> LOL a -> LOL a |
|
|
lolCons x (LOL p xs) = LOL p (x:xs) |
|
|
lolCons x (LOL p xs) = LOL p (x:xs) |
|
|
|
|
|
|
|
|
makeTupleType [x] = ParenType x |
|
|
|
|
|
|
|
|
makeTupleType [x] = |
|
|
|
|
|
case x of |
|
|
|
|
|
SPType ParenType{} _ _ -> x |
|
|
|
|
|
_ -> ParenType x |
|
|
makeTupleType xs = Tytup xs |
|
|
makeTupleType xs = Tytup xs |
|
|
|
|
|
|
|
|
makeTuplePattern [x] = ParenPat x |
|
|
|
|
|
|
|
|
makeTuplePattern [x] = |
|
|
|
|
|
case x of |
|
|
|
|
|
SPPat ParenPat{} _ _ -> x |
|
|
|
|
|
_ -> ParenPat x |
|
|
makeTuplePattern xs = TupPat xs |
|
|
makeTuplePattern xs = TupPat xs |
|
|
|
|
|
|
|
|
makeTuple [x] = ParenExp x |
|
|
|
|
|
|
|
|
makeTuple [x] = |
|
|
|
|
|
case x of |
|
|
|
|
|
SPExpr ParenExp{} _ _ -> x |
|
|
|
|
|
_ -> ParenExp x |
|
|
makeTuple xs = Tuple xs |
|
|
makeTuple xs = Tuple xs |
|
|
|
|
|
|
|
|
spanModuleItems xs = do |
|
|
spanModuleItems xs = do |
|
@ -273,4 +360,12 @@ spanModuleItems xs = do |
|
|
_ -> pure () |
|
|
_ -> pure () |
|
|
|
|
|
|
|
|
pure (map itemImport imports, items) |
|
|
pure (map itemImport imports, items) |
|
|
|
|
|
|
|
|
|
|
|
tupleConSymName :: Int -> T.Text |
|
|
|
|
|
tupleConSymName n = T.singleton '(' <> T.replicate n (T.singleton ',') <> T.singleton ')' |
|
|
|
|
|
|
|
|
|
|
|
endOfListPos :: HasPosn x => x -> [x] -> Posn |
|
|
|
|
|
endOfListPos x [] = endPosn x |
|
|
|
|
|
endOfListPos _ xs = endPosn (last xs) |
|
|
|
|
|
|
|
|
} |
|
|
} |