Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions pkg_parser/ast.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
///|
pub(all) enum Ast {
Null(Location)
False(Location)
True(Location)
Str(str~ : String, loc~ : Location)
Float(float~ : String, loc~ : Location)
Arr(content~ : Array[Ast], loc~ : Location)
Obj(map~ : Map[String, Ast], loc~ : Location)
} derive(Debug, Eq)

///|
pub fn Ast::loc(self : Self) -> Location {
match self {
Null(loc) => loc
False(loc) => loc
True(loc) => loc
Str(loc~, ..) => loc
Float(loc~, ..) => loc
Arr(loc~, ..) => loc
Obj(loc~, ..) => loc
}
}

///|
pub fn Ast::as_bool(self : Self) -> Bool? {
match self {
True(_) => Some(true)
False(_) => Some(false)
_ => None
}
}

///|
pub fn Ast::as_string(self : Self) -> String? {
match self {
Str(str~, ..) => Some(str)
_ => None
}
}

///|
pub fn Ast::as_number(self : Self) -> String? {
match self {
Float(float~, ..) => Some(float)
_ => None
}
}

///|
pub fn Ast::as_array(self : Self) -> Array[Ast]? {
match self {
Arr(content~, ..) => Some(content)
_ => None
}
}

///|
pub fn Ast::as_object(self : Self) -> Map[String, Ast]? {
match self {
Obj(map~, ..) => Some(map)
_ => None
}
}

///|
pub impl ToJson for Ast with to_json(self) {
match self {
Null(loc) => { "type": "Null", "loc": loc }
False(loc) => { "type": "False", "loc": loc }
True(loc) => { "type": "True", "loc": loc }
Str(str~, loc~) => { "type": "Str", "str": str, "loc": loc }
Float(float~, loc~) => { "type": "Float", "float": float, "loc": loc }
Arr(content~, loc~) => { "type": "Arr", "content": content, "loc": loc }
Obj(map~, loc~) => { "type": "Obj", "map": map, "loc": loc }
}
}
5 changes: 5 additions & 0 deletions pkg_parser/imports.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
///|
using @basic {type Location, type Position, type Report}

///|
using @tokens {type Token, type TokenKind, type Triple, type Triples}
11 changes: 11 additions & 0 deletions pkg_parser/moon.pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
"moonbitlang/parser/basic",
"moonbitlang/parser/tokens",
"moonbitlang/parser/lexer",
"moonbitlang/x/fs",
}

import {
"moonbitlang/core/test",
"moonbitlang/core/json",
} for "test"
Loading
Loading