Proposal: Introduce some hook to change encoding of parsed result
还没有人认领这个 Issue。
评估
调研方向
Start at the Spitfire.parse! entry point and trace how parsed nodes are constructed and returned, including the accumulator discussion linked in the issue. Define the encoder and accumulator contract, preserve the default Macro.t() result, and add coverage for custom AST output and the agreed performance boundary.
由索引模型根据 Issue 内容生成。
描述
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.
Proposal
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: %{...}
}
Context
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.
Additional Considerations
- The perhaps obvious alternative is to transform Elixir AST into whatever format you want after the fact. This has two downsides that I can think of:
- It is slower to parse, walk, and transform than it would be to parse and output the desired result in one shot.
- There is additional metadata/context during parsing that is not included in the Elixir AST but that could be valuable. As a concrete example: when parsing
Foo.Bar.\nBaz(note the newline), it's not possible to determine which lineBaroccurs on without inspecting the source, but with token data, it would be.
- It could be valuable to allow this or another hook to maintain and return an accumulator as well. This might be used to collect lint violations (in additional to parse errors). Based on this comment, it seems like you're already planning to return an accumulated value in addition to the parse result.
- I expect, if implemented, parsing in the default case would be measurably slower due to the overhead of the additional call whenever a node is being constructed. I'm not sure what an acceptable amount of performance loss is, but I acknowledge that there is a line somewhere. (My gut says something like 1.1-1.2x would be acceptable, while 2x would almost certainly not be.)
- 主要语言
- Elixir
- 星标
- 103
- 派生
- 15
- 平均合并
- 1 小时 20 分钟
- 30 天内合并 PR
- 2
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
elixir-tools/spitfire 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 62/100
elixir-tools/spitfire#136 ·
-
难度 3/5 1-2 天 新手友好度 68/100
elixir-tools/spitfire#135 · 4 条评论 ·
-
难度 4/5 3-5 天 新手友好度 42/100
elixir-tools/spitfire#127 ·
-
难度 3/5 1-2 天 新手友好度 45/100
elixir-tools/spitfire#119 ·
-
难度 5/5 一周以上 新手友好度 25/100
elixir-tools/spitfire#31 ·
查看 elixir-tools/spitfire 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 77/100
philss/rustler_precompiled#114 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
enhancement
难度 1/5 1 小时以内 新手友好度 76/100
edgehog-device-manager/edgehog#1715 ·
维护者通常 1 天内回复