Skip to content

Commit

Permalink
Modify parts of syntax and fix a bug
Browse files Browse the repository at this point in the history
Add literal pattern syntax

1. [TODO] That EDSL type level lang needs
   an update to make it being more compact and
   more meaningful.
2. [TODO] There is an issue for error report.
   No method to backtrack parsing path.
3. Fix a bug:
   "\ 2 = 3" and "[ 2 -> ?a = a ]" now can be parsed
   with no problem.
  • Loading branch information
dunor committed Jul 15, 2023
1 parent 19d57ba commit fd5cff4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bootstrap/src/Driver/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type PatLang e m typ expr ltyp lexpr = WithPattern e m
:- "binding"
:- "variable"
:- Layer "view" lexpr expr
:- "group" :- "variant" :- "record" :- "tuple"
:- "group" :- "variant" :- "record" :- "tuple" :- "literal"
:- Layer "annotation" ltyp typ
:- "operator"
)
Expand Down
6 changes: 3 additions & 3 deletions bootstrap/src/Tlang/Graph/Extension/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ data Bind name = Bind Flag Integer (Maybe name) deriving (Show, Eq, Ord)
-- if `Lock` is taken as relation, then it is treated the same as `Rigid`
-- if `Explicit` is taken as permission, then it is treated the same as `Flexible`
data Flag
= Explicit -- ^ permission f, Flexible
| Flexible -- ^ permission f, Flexible
| Rigid -- ^ permission r, Rigid
= Explicit -- ^ permission f, Flexible, user provided
| Flexible -- ^ permission f, Flexible, automatic generated
| Rigid -- ^ permission r, Rigid , automatic generated
deriving (Show, Eq, Ord)

-- | Permission for each node
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/src/Tlang/Parser/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ instance ( ExprC e m, Apply :<: f
(Expr f name) m where
tokenize _ parser end = do
let iPat = pratt @proxy @(pat (Expr f name))
(void . lookAhead . choice $ reservedOp <$> [",", "=>"]) Go
(void . lookAhead . choice $ reservedOp <$> [",", "="]) Go
<?> "Lambda Parameter pattern"
gPat = iPat `sepBy1` reservedOp "," <&> Grp
branch = (,) <$> gPat <*> (reservedOp "=>" *> parser (void $ lookAhead end) Go)
branch = (,) <$> gPat <*> (reservedOp "=" *> parser (void $ lookAhead end) Go)
lambda = do
heads <- Equation . Prefixes . fromMaybe [] <$> optional
(try $ ((:> (TypPht :: Type tbind trep tname a)) . Name <$> identifier) `manyTill` reservedOp ";;")
Expand Down
11 changes: 10 additions & 1 deletion bootstrap/src/Tlang/Parser/Pattern.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ instance (PatternC e m, label ~ Label, HasReader "PatternOperator" [Operator Tex
_ -> fail $ "Wrong position of " <> show op <> ": it has fixity " <> show fixity <> " but expect Infix, Postfix or Unifix"
return (Semantic nud' led' (return $ Power l))

-- | literal pattern
instance (PatternC e m, LiteralText :<: lit, LiteralNumber :<: lit, LiteralInteger :<: lit)
=> PrattToken (WithPattern e m "literal") (Pattern lit ext label name expr) m where
tokenize _ _ _ = do
lit <- try (float <&> PatPrm . inj . LiteralNumber . Literal)
<|> (integer <&> PatPrm . inj . LiteralInteger . Literal)
<|> (stringLiteral <&> PatPrm . inj . LiteralText . Literal)
return $ literal lit

-- | `@` pattern
instance (PatternC e m, name ~ Name, label ~ Label, LiteralText :<: lit, LiteralNumber :<: lit, LiteralInteger :<: lit)
=> PrattToken (WithPattern e m "binding") (Pattern lit ext label name expr) m where
Expand All @@ -114,7 +123,7 @@ instance (PatternC e m, name ~ Name, label ~ Label, LiteralText :<: lit, Literal
<|> reserved "_" $> PatWild -- wild pattern
<|> (char '?' <|> char '!') *> fmap (PatVar . Name) identifier -- variable
<|> PatSym . Label <$> identifier <*> return [] -- a constructor
<|> (float <&> PatPrm . inj . LiteralNumber . Literal)
<|> try (float <&> PatPrm . inj . LiteralNumber . Literal)
<|> (integer <&> PatPrm . inj . LiteralInteger . Literal)
<|> (stringLiteral <&> PatPrm . inj . LiteralText . Literal)
<|> (fmap PatTup . parens $ sepBy (parser (lookAhead $ (reservedOp "," <|> reservedOp ")") $> ()) Go) (reservedOp ","))
Expand Down

0 comments on commit fd5cff4

Please sign in to comment.