Skip to content

moonbitlang/parser

Repository files navigation

moonbitlang/parser

Note: This module is highly experimental and unstable. Use with caution.

A parser implementation for the MoonBit programming language, written in MoonBit, providing lexical analysis and syntax parsing capabilities.

This module defines the Abstract Syntax Tree (AST) and provides parsers for MoonBit source code. It includes both a hand-written LL(k) parser with error recovery and an LR(1) parser generated by moonyacc.

Project Structure

.
├── 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

Usage

///|
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": [],
            },
          },
        },
      },
    },
  ])
}

You can also explore the parsing results using the AST Explorer.

Build & Test

This project uses the MoonBit build system:

moon update
moon check
moon test

Contributing

We welcome contributions to this project! Please see CONTRIBUTING.md for guidelines on how to contribute and report bugs.

Releases

No releases published

Packages

No packages published

Contributors 5