Skip to content

Commit 9153680

Browse files
committed
add pkg parser
1 parent 393b96e commit 9153680

6 files changed

Lines changed: 1042 additions & 0 deletions

File tree

pkg_parser/ast.mbt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
///|
2+
pub(all) enum Ast {
3+
Null(Location)
4+
False(Location)
5+
True(Location)
6+
Str(str~ : String, loc~ : Location)
7+
Float(float~ : String, loc~ : Location)
8+
Arr(content~ : Array[Ast], loc~ : Location)
9+
Obj(map~ : Map[String, Ast], loc~ : Location)
10+
} derive(Debug, Eq)
11+
12+
///|
13+
pub fn Ast::loc(self : Self) -> Location {
14+
match self {
15+
Null(loc) => loc
16+
False(loc) => loc
17+
True(loc) => loc
18+
Str(loc~, ..) => loc
19+
Float(loc~, ..) => loc
20+
Arr(loc~, ..) => loc
21+
Obj(loc~, ..) => loc
22+
}
23+
}
24+
25+
///|
26+
pub fn Ast::as_bool(self : Self) -> Bool? {
27+
match self {
28+
True(_) => Some(true)
29+
False(_) => Some(false)
30+
_ => None
31+
}
32+
}
33+
34+
///|
35+
pub fn Ast::as_string(self : Self) -> String? {
36+
match self {
37+
Str(str~, ..) => Some(str)
38+
_ => None
39+
}
40+
}
41+
42+
///|
43+
pub fn Ast::as_number(self : Self) -> String? {
44+
match self {
45+
Float(float~, ..) => Some(float)
46+
_ => None
47+
}
48+
}
49+
50+
///|
51+
pub fn Ast::as_array(self : Self) -> Array[Ast]? {
52+
match self {
53+
Arr(content~, ..) => Some(content)
54+
_ => None
55+
}
56+
}
57+
58+
///|
59+
pub fn Ast::as_object(self : Self) -> Map[String, Ast]? {
60+
match self {
61+
Obj(map~, ..) => Some(map)
62+
_ => None
63+
}
64+
}
65+
66+
///|
67+
pub impl ToJson for Ast with to_json(self) {
68+
match self {
69+
Null(loc) => { "type": "Null", "loc": loc }
70+
False(loc) => { "type": "False", "loc": loc }
71+
True(loc) => { "type": "True", "loc": loc }
72+
Str(str~, loc~) => { "type": "Str", "str": str, "loc": loc }
73+
Float(float~, loc~) => { "type": "Float", "float": float, "loc": loc }
74+
Arr(content~, loc~) => { "type": "Arr", "content": content, "loc": loc }
75+
Obj(map~, loc~) => { "type": "Obj", "map": map, "loc": loc }
76+
}
77+
}

pkg_parser/imports.mbt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
///|
2+
using @basic {type Location, type Position, type Report}
3+
4+
///|
5+
using @tokens {type Token, type TokenKind, type Triple, type Triples}

pkg_parser/moon.pkg

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
"moonbitlang/parser/basic",
3+
"moonbitlang/parser/tokens",
4+
"moonbitlang/parser/lexer",
5+
"moonbitlang/x/fs",
6+
}
7+
8+
import {
9+
"moonbitlang/core/test",
10+
"moonbitlang/core/json",
11+
} for "test"

0 commit comments

Comments
 (0)