bogdanp / elm-ast Goto Github PK
View Code? Open in Web Editor NEWA parser for Elm in Elm.
Home Page: http://bogdanp.github.io/elm-ast/example/
License: BSD 3-Clause "New" or "Revised" License
A parser for Elm in Elm.
Home Page: http://bogdanp.github.io/elm-ast/example/
License: BSD 3-Clause "New" or "Revised" License
typeFun a = 0 -- Gets treated as type declaration
asFun a = 0 -- won't compile
letter = 'a' -- won't compile
Below is an example from List module.
toIndexedList : Array a -> List (Int, a)
toIndexedList array =
List.map2
(,)
(List.range 0 (Native.Array.length array - 1))
(Native.Array.toList array)
Just playing with the live demo, and I noticed i = (*) 2
can't be parsed, so I thought I'd log it. :-)
type X = A {}
The parser result is a list of statements at the moment. Are there any plans to add a stricter/richer type or type alias to only support valid combinations of statements, like only one module declaration at the top of the file? This is partially related to the question in #7. Or should something like this be done in some elm-ast-extra package?
fu n =
case n of
_ -> 0
Fails to parse.
This works:
fu n =
case n of
x -> 0
These also fail:
a _ = 0
b = (\_ -> 0)
c =
let
_ = 0
in
0
The following declaration in the parser:
x = "\\"
shows the following error:
(Err (["expected end of input"]),{ input = "x = "\\"\n", position = 0 })
In fact, any number of backslashes followed by a single quote fails to parse. While some of those are legitimate errors (odd numbers of backslashes), others aren't (even numbers of backslashes).
Hi! I noticed an incorrect AST when parsing something along the likes of
foo =
case pattern of
A -> 1
B ->
case variable of
C -> 2
_ -> 3
_ ->
4
Which results in an AST like the following
ast =
Case (Variable [ "pattern" ])
([ ( Variable [ "A" ]
, Integer 1
)
, ( Variable [ "B" ]
, Case
(Variable [ "variable" ])
([ ( Variable [ "C" ]
, Integer 2
)
, ( Variable [ "_" ], Integer 3 )
, ( Variable [ "_" ], Integer 4 )
]
)
)
]
)
I expect the first case expression to have 3 patterns (A, B and _ ) and the nested one under B to have 2 (C and _ ), but they respectively have 2 ((A, B) and (C, _ and _ ).
It seems like {a = a, b = b}
should parse to
Record [("a", (Variable ["a"])), ("b", (Variable ["b"]))]
but it causes the errors
failed to parse: {a = a, b = b} at position 0 with errors: ["expected \"let\"","expected \"case\"","expected \"if\"","expected \"\\\\\"","expected '\\''","expected \"\\\"\\\"\\\"\"","expected \"\\\"\"","expected a float","expected an integer","expected \"_\"","expected a lowercase character","expected an uppercase character","expected \"_\"","expected a
lowercase character","expected an uppercase character","expected \"[\"","expected \"[\"","expected \"}\"","expected \"(\""]
a b c d e f g h i j k l m
Compiles instantly
while
a
b
c
d
e
f
g
h
i
j
k
l
m
Would take couple of minutes
So we can write expressions like:
(a, b |> toString)
that wouldn't compile to
((a,b) |> toString
module Main exposing ((?))
This code does not parse.
Super cool project! ๐
Here's a little bug I found:
f =
let
foo b =
b
in
1
-- (Err (["expected end of input"]),{ input = "f x =\n let\n foo b =\n b\n in\n 1\n", position = 42 })
While testing your great library I've found this case:
import Html exposing (..)
main =
div []
[ text "Hello, World!" ]
that compiles on http://elm-lang.org/try, but does not parse on http://bogdanp.github.io/elm-ast/example/.
After modifying to div [] [ text "Hello, World!" ]
(one line) code parses correctly.
Not sure do you work on full compatibility, but wanted to report you this issue.
{a: a}
== {a}
a = a { rec | a = 1 } b
Taken extracted from https://raw.githubusercontent.com/rundis/elm-bootstrap/3.0.0/src/Bootstrap/Popover.elm
calculatePos pos { rect, offsetWidth, offsetHeight } =
{ left = -offsetWidth
, top = (rect.height / 2) - (offsetHeight / 2)
}
'\t'
'\n'
'\\'
'\x00'
etc
example:
let
letter = 'a'
in
0
map .someField listOfRecords
f =
let b = 1 --comment
in b
The parser does not understand this.
This is probably related to #1, because this, too has to do with newlines.
Example:
module Main exposing (..)
a =
case a of
a ->
a :: "A" ++ 2
Hi! First of all, thanks for making/picking up the package! โค๏ธ
I noticed that parsing fails when I'm pattern matching, and I named one of the variables alias
(which is valid Elm code).
This fails:
fn param =
case param of
A alias -> 1
B -> 2
This works:
fn param =
case param of
A aliass -> 1
B -> 2
I found that the package.elm-lang.org frontend perfectly solves the problem for which I have used this library. The type parsing code there might be helpful in this project.
https://github.com/elm-lang/package.elm-lang.org/blob/master/src/frontend/Parse/Type.elm
I've found parsing error on some simple code in my fork and tested that this project suffers it too (as long as example page is up to date). This example is extracted from Html module:
node =
VirtualDom.node
aside =
node "aside"
Even simpler (seems same case):
node = 0
aside = 7
What's interesting after switching order of functions code parses correctly.
{ a = Just 1 }
right now tries to be interpreted as
{ (a = Just) 1}
typeAttribute = Attributes.type_
> 0x000000
0 : number
> 0x0000001
1 : number
> 0x0000201
513 : number
> 0x000020a
522 : number
I could be wrong, but it seems like [a, b]
should parse to
List [Variable ["a"], Variable ["b"]]
but it parses to
List ([BinOp (Variable [","]) (Variable ["a"]) (Variable ["b"])])
and it gets more complicated as you add more elements.
Since a FunctionTypeDeclaration
without a corresponding FunctionDeclaration
is not legal (syntax?) in Elm, would it be useful to change:
type Statement
= ...
| FunctionTypeDeclaration Name Type
| FunctionDeclaration Name (List Expression) Expression
| ...
to:
type alias FunctionTypeDeclaration = FunctionTypeDeclaration Name Type
type alias FunctionDeclaration = FunctionDeclaration Name (List Expression) Expression
type Statement
= ...
| FunctionDefinition
(Maybe FunctionTypeDeclaration, FunctionDeclaration)
| ...
with corresponding tweak to parser rules?
Thanks for writing this! I think this will be very useful for an idea I have for converting GraphQL schemas to Elm code. However one key thing that seems to be missing is an AST printer. Is that planned? If not I can try to come up with something.
I just discovered this and thought, Elm could be another supported language here: http://astexplorer.net/
t : (Int, Type -> Type)
Won't work unless a function is wrapped in parens
t : (Int, (Type -> Type))
Apologies if this channel is inappropriate, I'm trying to understand the code. Thanks!
Parsing of effects module fails. Example:
effect module Task where { command = MyCmd } exposing
Removing hasImport : List String -> Bool
helps, yet this is valid elm code:
compile name =
let
hasImport : List String -> Bool
hasImport name = True
in
hasImport name
fn = .field
{- generates
AccessFunction [ field ]
which is only a syntactic sugar for:
-}
fn = \x -> x.field
{- that generates
Lambda [ Variable [ "x" ] ] (Access (Variable [ "x" ]) [ field ])
`x` variable used in example has to be different from `field`;
may be generated like field ++ "_"
-}
So it seems that additional algebraic element is not needed here. For sake of (model) simplicity it could be removed.
This works
(a, b)
But this doesn't
( a, b )
Which can be a problem, given that the latter is the default output of elm-format
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.