-
-
Notifications
You must be signed in to change notification settings - Fork 74
example converter from json to ast #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #if NET5_0_OR_GREATER | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Esprima.Ast; | ||
|
|
||
| namespace Esprima.Utils | ||
| { | ||
| public class AstJsonConverter : JsonConverter<object> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we keep this sealed until someone asks for it to be opened, generally I guess we should be able to reproduce the original JSON AST for this to be "feature-complete". |
||
| { | ||
| public override bool CanConvert(Type typeToConvert) | ||
| { | ||
| return typeof(Node).IsAssignableFrom(typeToConvert); | ||
| } | ||
|
|
||
| public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| var propertyBag = new Dictionary<string, object>(); | ||
| reader.Read(); | ||
| while (true) | ||
| { | ||
| if (reader.TokenType != JsonTokenType.PropertyName && reader.TokenType != JsonTokenType.String) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| var name = reader.GetString(); | ||
| reader.Read(); | ||
|
|
||
| object value = null; | ||
| if (reader.TokenType == JsonTokenType.String) | ||
| { | ||
| value = reader.GetString(); | ||
| reader.Read(); | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.Number) | ||
| { | ||
| value = reader.GetDouble(); | ||
| reader.Read(); | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.True) | ||
| { | ||
| value = true; | ||
| reader.Read(); | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.False) | ||
| { | ||
| value = false; | ||
| reader.Read(); | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.Null) | ||
| { | ||
| value = null; | ||
| reader.Read(); | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.StartArray) | ||
| { | ||
| var values = new List<Node>(); | ||
| reader.Read(); | ||
| while (true) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.EndArray) | ||
| break; | ||
| var v = (Node) JsonSerializer.Deserialize(ref reader, typeof(Node), options); | ||
| reader.Read(); | ||
| values.Add(v); | ||
| } | ||
| reader.Read(); | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.StartObject) | ||
| { | ||
| value = JsonSerializer.Deserialize(ref reader, typeof(Node), options); | ||
| reader.Read(); | ||
| } | ||
| else | ||
| { | ||
| reader.Read(); | ||
| } | ||
| propertyBag.Add(name, value); | ||
| } | ||
|
|
||
| var type = (string)propertyBag["type"]; | ||
| switch (type) | ||
| { | ||
| case "AssignmentExpression": | ||
| return new AssignmentExpression((string) propertyBag["op"], (Expression) propertyBag["left"], (Expression) propertyBag["right"]); | ||
| case "ArrayExpression": | ||
| return new ArrayExpression(NodeList.Create(((List<Node>) propertyBag["elements"]).Cast<Expression>())); | ||
| case "BlockStatement": | ||
| return new BlockStatement(NodeList.Create(((List<Node>) propertyBag["elements"]).Cast<Statement>())); | ||
| case "BinaryExpression": | ||
| return new BinaryExpression((string) propertyBag["op"], (Expression) propertyBag["left"], (Expression) propertyBag["right"]); | ||
| case "Identifier": | ||
| return new Identifier((string) propertyBag["name"]); | ||
| case "Literal": | ||
| { | ||
| var value = propertyBag["value"]; | ||
| if (value is bool) | ||
| return new Literal((bool) value, (string) propertyBag["raw"]); | ||
| if (value is double) | ||
| return new Literal((double) value, (string) propertyBag["raw"]); | ||
| return new Literal((string) value, (string) propertyBag["raw"]); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #if NET5_0_OR_GREATER | ||
| using System.Text.Json; | ||
| using Esprima.Ast; | ||
| using Esprima.Utils; | ||
| using Xunit; | ||
|
|
||
| namespace Esprima.Tests | ||
| { | ||
| public class JsonTest | ||
| { | ||
| [Fact] | ||
| public void JsonToAstParserTest() | ||
| { | ||
| var parser = new JavaScriptParser(@"if (true) { p(); } | ||
| switch(foo) { | ||
| case 'A': | ||
| p(); | ||
| break; | ||
| } | ||
| switch(foo) { | ||
| default: | ||
| p(); | ||
| break; | ||
| } | ||
| for (var a = []; ; ) { } | ||
| for (var elem of list) { } | ||
| "); | ||
| var program = parser.ParseScript(); | ||
| var json = AstJson.ToJsonString(program); | ||
|
|
||
| var op = new JsonSerializerOptions(); | ||
| op.Converters.Add(new AstJsonConverter()); | ||
| var ast = JsonSerializer.Deserialize<Node>(json, op); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should the result be asserted? some acceptance criteria library probably could help here for text matching / diffing. |
||
| } | ||
| } | ||
| } | ||
| #endif | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can now use net6.0 too