Skip to content

Commit

Permalink
Merge pull request #1 from laslowh/feature/meta
Browse files Browse the repository at this point in the history
Attempt to complete location information for expressions
  • Loading branch information
baransu committed Sep 27, 2017
2 parents 47c0c41 + 0c55070 commit 341df49
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion example/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ expression e =
List es _ ->
withChild e (List.map expression es)

Application e1 e2 ->
Application e1 e2 m ->
withChild e
[ expression e1
, expression e2
Expand Down
66 changes: 33 additions & 33 deletions src/Ast/Expression.elm
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ type Expression
| Let (List ( Expression, Expression )) Expression Meta
| Case Expression (List ( Expression, Expression )) Meta
| Lambda (List Expression) Expression Meta
-- missing meta yet
| Variable (List Name)
| Application Expression Expression
| BinOp Expression Expression Expression
| Variable (List Name) Meta
| Application Expression Expression Meta
| BinOp Expression Expression Expression Meta


character : Parser s Expression
Expand Down Expand Up @@ -142,11 +141,10 @@ accessFunction =

variable : Parser s Expression
variable =
Variable
withMeta <| Variable
<$> choice
[ singleton <$> loName
, sepBy1 (Combine.string ".") upName
, singleton <$> parens operator
, singleton <$> parens (Combine.regex ",+")
, singleton <$> emptyTuple
]
Expand Down Expand Up @@ -191,15 +189,14 @@ simplifiedRecord =
Record
<$> (braces
(commaSeparated
((\a ->
(withMeta ((\a -> (\m ->
( a
, Variable [ a ]
, Variable [ a ] m
)
)
<$> loName
))
<$> loName))
)
)
)


recordUpdate : OpTable -> Parser s Expression
Expand Down Expand Up @@ -276,10 +273,11 @@ lambda ops =

application : OpTable -> Parser s Expression
application ops =
let flippedapp m e1 e2 = Application e1 e2 m in
lazy <|
\() ->
chainl
(Application <$ spacesOrIndentedNewline ops)
(withMeta ((\m->flippedapp m) <$ spacesOrIndentedNewline ops))
(term ops)


Expand Down Expand Up @@ -310,12 +308,11 @@ spacesOrIndentedNewline ops =
(regex "[ \\t]*\n[ \\t]+" *> maybeBindingAhead ops) <|> spaces_


operatorOrAsBetween : Parser s String
operatorOrAsBetween : Parser s Operator
operatorOrAsBetween =
lazy <|
\() ->
between_ whitespace <| operator <|> symbol_ "as"

between_ whitespace <| operator <|> withMeta ((,) <$> symbol_ "as")

binary : OpTable -> Parser s Expression
binary ops =
Expand Down Expand Up @@ -397,12 +394,12 @@ level ops n =
Tuple.second <| op ops n


hasLevel : OpTable -> Int -> ( String, Expression ) -> Bool
hasLevel ops l ( n, _ ) =
hasLevel : OpTable -> Int -> String -> Bool
hasLevel ops l n =
level ops n == l


split : OpTable -> Int -> Expression -> List ( String, Expression ) -> Parser s Expression
split : OpTable -> Int -> Expression -> List ( Operator, Expression ) -> Parser s Expression
split ops l e eops =
case eops of
[] ->
Expand All @@ -416,9 +413,9 @@ split ops l e eops =
let
ops_ =
List.filterMap
(\x ->
if hasLevel ops l x then
Just (Tuple.first x)
(\(x,_) ->
if hasLevel ops l (Tuple.first x) then
Just x
else
Nothing
)
Expand All @@ -434,28 +431,29 @@ split ops l e eops =
)


splitLevel : OpTable -> Int -> Expression -> List ( String, Expression ) -> List (Parser s Expression)
splitLevel : OpTable -> Int -> Expression -> List ( Operator, Expression ) -> List (Parser s Expression)
splitLevel ops l e eops =
case break (hasLevel ops l) eops of
case break ((hasLevel ops l) << Tuple.first << Tuple.first) eops of
( lops, ( _, e_ ) :: rops ) ->
split ops (l + 1) e lops :: splitLevel ops l e_ rops

( lops, [] ) ->
[ split ops (l + 1) e lops ]


joinL : List Expression -> List String -> Parser s Expression
joinL : List Expression -> List Operator -> Parser s Expression
joinL es ops =
case ( es, ops ) of
( [ e ], [] ) ->
succeed e

( a :: b :: remE, op :: remO ) ->
( a :: b :: remE, (op,m) :: remO ) ->
joinL
((BinOp
(Variable [ op ])
(Variable [op] m)
a
b
m
)
:: remE
)
Expand All @@ -465,40 +463,42 @@ joinL es ops =
fail ""


joinR : List Expression -> List String -> Parser s Expression
joinR : List Expression -> List Operator -> Parser s Expression
joinR es ops =
case ( es, ops ) of
( [ e ], [] ) ->
succeed e

( a :: b :: remE, op :: remO ) ->
( a :: b :: remE, (op,m) :: remO ) ->
joinR (b :: remE) remO
>>= (\e ->
succeed
(BinOp
(Variable [ op ])
(Variable [op] m)
a
e
m
)
)

_ ->
fail ""


findAssoc : OpTable -> Int -> List ( String, Expression ) -> Parser s Assoc
findAssoc : OpTable -> Int -> List ( Operator, Expression ) -> Parser s Assoc
findAssoc ops l eops =
let
bareops = List.map (Tuple.first << Tuple.first) eops
lops =
List.filter (hasLevel ops l) eops
List.filter (hasLevel ops l) bareops

assocs =
List.map (assoc ops << Tuple.first) lops
List.map (assoc ops) bareops

error issue =
let
operators =
List.map Tuple.first lops |> String.join " and "
bareops |> String.join " and "
in
"conflicting " ++ issue ++ " for operators " ++ operators
in
Expand Down
6 changes: 3 additions & 3 deletions src/Ast/Helpers.elm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type alias Meta =
, column : Int
}

type alias Operator = (String, Meta)

makeMeta : ParseLocation -> Meta
makeMeta { line, column } =
Expand Down Expand Up @@ -139,8 +140,7 @@ emptyTuple : Parser s String
emptyTuple =
string "()"


operator : Parser s String
operator : Parser s Operator
operator =
lazy <|
\() ->
Expand All @@ -149,7 +149,7 @@ operator =
if List.member n reservedOperators then
fail <| "operator '" ++ n ++ "' is reserved"
else
succeed n
withLocation (\l -> succeed (n,makeMeta l))
)


Expand Down
8 changes: 4 additions & 4 deletions src/Ast/Statement.elm
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ allExport =

functionExport : Parser s ExportSet
functionExport =
FunctionExport <$> choice [ functionName, parens operator ]
FunctionExport <$> choice [ functionName, map (Tuple.first) (parens operator) ]


constructorSubsetExports : Parser s ExportSet
Expand Down Expand Up @@ -319,15 +319,15 @@ functionTypeDeclaration : Parser s Statement
functionTypeDeclaration =
withMeta <|
FunctionTypeDeclaration
<$> (choice [ loName, parens operator ] <* symbol ":")
<$> (choice [ loName, map (Tuple.first) (parens operator) ] <* symbol ":")
<*> typeAnnotation


functionDeclaration : OpTable -> Parser s Statement
functionDeclaration ops =
withMeta <|
FunctionDeclaration
<$> (choice [ loName, parens operator ])
<$> (choice [ loName, map (Tuple.first) (parens operator) ])
<*> (many (between_ whitespace <| term ops))
<*> (symbol "=" *> whitespace *> expression ops)

Expand All @@ -347,7 +347,7 @@ infixDeclaration =
, N <$ initialSymbol "infix"
]
<*> (spaces *> Combine.Num.int)
<*> (spaces *> (loName <|> operator))
<*> (spaces *> (loName <|> map (Tuple.first) operator))



Expand Down

0 comments on commit 341df49

Please sign in to comment.