elixir-tools / spitfire Goto Github PK
View Code? Open in Web Editor NEWError tolerant parser for Elixir
Home Page: https://www.elixir-tools.dev
License: MIT License
Error tolerant parser for Elixir
Home Page: https://www.elixir-tools.dev
License: MIT License
alias Foo.{Bar, Baz}
alias Foo.{
Bar,
Baz
}
This snippet is handled just fine by Code.string_to_quoted/2
but Spitfire returns an error
defmodule Foo do
@possible_results [:a, :b, :c]
@type result_type ::
unquote(
for result <- @possible_results, reduce: [] do
acc -> {:|, [], [result, acc]}
end
)
end
Hi Mitch! First off, thanks for your work on Spitfire. It's shaping up really well and is already a valuable tool!
Second, to hedge a bit, I don't know whether implementing this proposal would be appropriate right now given Spitfire's current state of development, but I wanted to bring it up now regardless.
Introduce some hook, be it a callback, behaviour, or something else, that controls the encoding of the parsed result. The default would be to emit Macro.t()
, as Spitfire currently does, but the hook would allow parsing Elixir code into a bespoke AST of arbitrary format.
As a concrete example, you might imagine the following:
Spitfire.parse!("1 + 2.5", encoder: &custom_encoder/1) # not sure what actual encoder arity would be
#=>
%BinaryCall{
op: :+,
meta: %{...},
left: %Integer{value: 1, meta: %{...},
right: %Float{value: 2.5, meta: %{...}
}
Elixir's AST is intentionally minimal. One reason is to facilitate authoring macros. For example:
foo(x: 1, y: 2)
# parses to:
{:foo, [], [[{:x, 1}, {:y, 2}]]}
# as opposed to:
{:foo, [],
[[{:{}, [], [:x, 1]},
{:{}, [], [:y, 2]}]]}
This makes it trivial to use keyword lists as options in macros, but also means that code processing an AST has to handle two forms of tuple. This is only one example of where the default AST can be cumbersome, but there are many situations where complex pattern matching, guards, or even metadata inspection are required to precisely differentiate syntax.
Sourceror, for instance, has a long standing issue for an enriched AST.
Foo.Bar.\nBaz
(note the newline), it's not possible to determine which line Bar
occurs on without inspecting the source, but with token data, it would be.__MODULE__.Foo
__MODULE__.foo()
Currently the following snippet is parsed as a :__block__
by Code.string_to_quoted, but I'm not sure why
(!false)
I have asked a question to core to resolve what the behavior should be: elixir-lang/elixir#13324
trailing commas need to be parsed in many places
Spitfire doesn't handle
%__MODULE__.Foo{bar: "foo"}
Spitfire doesn't handle
foo[bar["baz"]]
Code.string_to_quoted/2 returns
{:ok,
{{:.,
[from_brackets: true, closing: [line: 1, column: 15], line: 1, column: 4],
[Access, :get]},
[from_brackets: true, closing: [line: 1, column: 15], line: 1, column: 4],
[
{:foo, [line: 1, column: 1], nil},
{{:.,
[from_brackets: true, closing: [line: 1, column: 14], line: 1, column: 8],
[Access, :get]},
[from_brackets: true, closing: [line: 1, column: 14], line: 1, column: 8],
[{:bar, [line: 1, column: 5], nil}, "baz"]}
]}}
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.