AST and parsers for the MoonBit programming language
Dependencies
.
├── tokens/ # token definitions and utilities
├── lexer/ # Lexical analysis components
├── mbti_ast/ # AST for .mbti files
├── mbti_parser/ # LR parser for .mbti files
├── syntax/ # AST and utils for MoonBit source code
├── handrolled_parser/ # Hand-written parser implementation
├── yacc_parser/ # LR parser generated by moonyacc
├── test/ # Test suite
└── top.mbt # parsing entries///|
test {
let source =
#| let number = 123
#| fn add(a: Int, b: Int) -> Int {
#| a + b
#| }
#| enum Color {
#| Red
#| Green
#| Blue
#| }
#| struct Point {
#| x: Int
#| y: Int
#| }
let (impls, _) = parse_string(source)
json_inspect(impls.map(impl_ => impl_.json_repr()), content=[
{
"kind": "Impl::TopLetDef",
"loc": null,
"children": {
"binder": {
"kind": "Binder",
"loc": null,
"children": { "name": "number" },
},
"ty": null,
"expr": {
"kind": "Expr::Constant",
"loc": null,
"children": {
"constant": {
"kind": "Constant::Int",
"loc": null,
"children": { "value": "123" },
},
},
},
"vis": { "kind": "Visibility::Default", "loc": null, "children": {} },
"is_constant": false,
"attrs": {
"kind": "Impl::TopLetDef::AttrList",
"loc": null,
"children": [],
},
"doc": "",
},
},
{
"kind": "Impl::TopFuncDef",
"loc": null,
"children": {
"fun_decl": {
"kind": "FunDecl",
"loc": null,
"children": {
"type_name": null,
"name": {
"kind": "Binder",
"loc": null,
"children": { "name": "add" },
},
"has_error": null,
"is_async": null,
"decl_params": {
"kind": "FunDecl::ParameterList",
"loc": null,
"children": [
{
"kind": "Parameter::Positional",
"loc": null,
"children": {
"binder": {
"kind": "Binder",
"loc": null,
"children": { "name": "a" },
},
"ty": {
"kind": "Type::Name",
"loc": null,
"children": {
"constr_id": {
"kind": "ConstrId",
"loc": null,
"children": {
"id": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "Int" },
},
},
},
"tys": {
"kind": "Type::Name::TypeList",
"loc": null,
"children": [],
},
},
},
},
},
{
"kind": "Parameter::Positional",
"loc": null,
"children": {
"binder": {
"kind": "Binder",
"loc": null,
"children": { "name": "b" },
},
"ty": {
"kind": "Type::Name",
"loc": null,
"children": {
"constr_id": {
"kind": "ConstrId",
"loc": null,
"children": {
"id": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "Int" },
},
},
},
"tys": {
"kind": "Type::Name::TypeList",
"loc": null,
"children": [],
},
},
},
},
},
],
},
"quantifiers": {
"kind": "FunDecl::QuantifierList",
"loc": null,
"children": [],
},
"return_type": {
"kind": "Type::Name",
"loc": null,
"children": {
"constr_id": {
"kind": "ConstrId",
"loc": null,
"children": {
"id": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "Int" },
},
},
},
"tys": {
"kind": "Type::Name::TypeList",
"loc": null,
"children": [],
},
},
},
"error_type": {
"kind": "ErrorType::NoErrorType",
"loc": null,
"children": {},
},
"vis": {
"kind": "Visibility::Default",
"loc": null,
"children": {},
},
"attrs": {
"kind": "FunDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
},
},
"decl_body": {
"kind": "DeclBody::DeclBody",
"loc": null,
"children": {
"local_types": {
"kind": "DeclBody::DeclBody::LocalTypeList",
"loc": null,
"children": [],
},
"expr": {
"kind": "Expr::Infix",
"loc": null,
"children": {
"op": {
"kind": "Var",
"loc": null,
"children": {
"name": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "+" },
},
},
},
"lhs": {
"kind": "Expr::Ident",
"loc": null,
"children": {
"id": {
"kind": "Var",
"loc": null,
"children": {
"name": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "a" },
},
},
},
},
},
"rhs": {
"kind": "Expr::Ident",
"loc": null,
"children": {
"id": {
"kind": "Var",
"loc": null,
"children": {
"name": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "b" },
},
},
},
},
},
},
},
},
},
},
},
{
"kind": "Impl::TopTypeDef",
"loc": null,
"children": {
"value": {
"kind": "TypeDecl",
"loc": null,
"children": {
"tycon": "Color",
"params": {
"kind": "TypeDecl::ParamList",
"loc": null,
"children": [],
},
"components": {
"kind": "TypeDesc::Variant",
"loc": null,
"children": {
"value": {
"kind": "TypeDesc::Variant::ConstrList",
"loc": null,
"children": [
{
"kind": "ConstrDecl",
"loc": null,
"children": {
"name": {
"kind": "ConstrName",
"loc": null,
"children": { "name": "Red" },
},
"args": null,
"tag": null,
"attrs": {
"kind": "ConstrDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
},
},
{
"kind": "ConstrDecl",
"loc": null,
"children": {
"name": {
"kind": "ConstrName",
"loc": null,
"children": { "name": "Green" },
},
"args": null,
"tag": null,
"attrs": {
"kind": "ConstrDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
},
},
{
"kind": "ConstrDecl",
"loc": null,
"children": {
"name": {
"kind": "ConstrName",
"loc": null,
"children": { "name": "Blue" },
},
"args": null,
"tag": null,
"attrs": {
"kind": "ConstrDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
},
},
],
},
},
},
"attrs": {
"kind": "TypeDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
"type_vis": {
"kind": "Visibility::Default",
"loc": null,
"children": {},
},
"deriving": {
"kind": "TypeDecl::DerivingList",
"loc": null,
"children": [],
},
},
},
},
},
{
"kind": "Impl::TopTypeDef",
"loc": null,
"children": {
"value": {
"kind": "TypeDecl",
"loc": null,
"children": {
"tycon": "Point",
"params": {
"kind": "TypeDecl::ParamList",
"loc": null,
"children": [],
},
"components": {
"kind": "TypeDesc::Record",
"loc": null,
"children": {
"value": {
"kind": "TypeDesc::Record::FieldList",
"loc": null,
"children": [
{
"kind": "FieldDecl",
"loc": null,
"children": {
"name": {
"kind": "FieldName",
"loc": null,
"children": { "label": "x" },
},
"ty": {
"kind": "Type::Name",
"loc": null,
"children": {
"constr_id": {
"kind": "ConstrId",
"loc": null,
"children": {
"id": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "Int" },
},
},
},
"tys": {
"kind": "Type::Name::TypeList",
"loc": null,
"children": [],
},
},
},
"mut": false,
"vis": {
"kind": "Visibility::Default",
"loc": null,
"children": {},
},
"attrs": {
"kind": "FieldDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
},
},
{
"kind": "FieldDecl",
"loc": null,
"children": {
"name": {
"kind": "FieldName",
"loc": null,
"children": { "label": "y" },
},
"ty": {
"kind": "Type::Name",
"loc": null,
"children": {
"constr_id": {
"kind": "ConstrId",
"loc": null,
"children": {
"id": {
"kind": "LongIdent::Ident",
"loc": null,
"children": { "value": "Int" },
},
},
},
"tys": {
"kind": "Type::Name::TypeList",
"loc": null,
"children": [],
},
},
},
"mut": false,
"vis": {
"kind": "Visibility::Default",
"loc": null,
"children": {},
},
"attrs": {
"kind": "FieldDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
},
},
],
},
},
},
"attrs": {
"kind": "TypeDecl::AttrList",
"loc": null,
"children": [],
},
"doc": "",
"type_vis": {
"kind": "Visibility::Default",
"loc": null,
"children": {},
},
"deriving": {
"kind": "TypeDecl::DerivingList",
"loc": null,
"children": [],
},
},
},
},
},
])
}moon update
moon check
moon testAST and parsers for the MoonBit programming language
Dependencies