Update to Pest grammar for hello world.

This commit is contained in:
Jesse Brault 2024-12-31 17:23:36 -06:00
parent db83cb7403
commit d4fb4680a5
3 changed files with 44 additions and 11 deletions

8
src/compiler/mod.rs Normal file
View File

@ -0,0 +1,8 @@
use crate::parser::{DeimosParser, Rule};
use crate::vm::lib::DmLib;
use pest::Parser;
pub fn compile(src_unit: &str) -> DmLib {
let p = DeimosParser::parse(Rule::compilation_unit, src_unit).unwrap();
todo!()
}

View File

@ -1,3 +1,4 @@
mod compiler;
pub mod parser;
mod util;
pub mod vm;

View File

@ -1,24 +1,31 @@
compilation_unit = { SOI ~ namespace? ~ declaration* ~ EOI }
namespace = { "ns" ~ fqn }
fqn = { identifier ~ ( "::" ~ fqn )* }
fqn = { identifier ~ ( "::" ~ identifier )* }
identifier = @{ identifier_start_char ~ identifier_char* }
identifier_start_char = { 'a'..'z' | 'A'..'Z' | "_" }
identifier_char = { 'a'..'z' | 'A'..'Z' | '0'..'9' | "_" }
declaration = { "decl"? ~ "pub"? ~ ( interface | module | property | function ) }
declaration = { decl? ~ pub? ~ ( interface | implementation | module | function ) }
decl = { "decl" }
pub = { "pub" }
interface = { "int" ~ identifier ~ generics_declaration? ~ interface_extends_list? ~ "{" ~ declaration* ~ "}"}
interface_extends_list = { ":" ~ type ~ ( "+" ~ type )* }
interface = { "int" ~ identifier ~ generics_declaration? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? }
extends_list = { ":" ~ type ~ ( "+" ~ type )* }
implementation = { "impl" ~ identifier ~ generics_declaration? ~ impl_ctor? ~ extends_list? }
impl_ctor = { "(" ~ impl_ctor_args? ~ ")" }
impl_ctor_args = { impl_ctor_arg ~ ( "," ~ impl_ctor_arg )* }
impl_ctor_arg = { fld? ~ identifier ~ ":" ~ type }
fld = { "fld" }
module = { "mod" ~ identifier ~ "{" ~ declaration* ~ "}" }
property = { identifier ~ ":" ~ type }
function = { "fn" ~ generics_declaration? ~ identifier ~ "(" ~ args_list? ~ ")" ~ ( ":" ~ type )? ~ ( "{" ~ "}" )? }
function = { "fn" ~ generics_declaration? ~ identifier ~ "(" ~ args_list? ~ ")" ~ ( ":" ~ type )? ~ ( function_body | function_equals_body ) }
function_equals_body = { "=" ~ expression }
function_body = { "{" ~ "}" }
function_body = { "{" ~ statement* ~ "}" }
type = { identifier ~ generics_declaration? }
generics_declaration = { "<" ~ identifier ~ ( "," ~ identifier )* ~ ">"}
@ -26,12 +33,29 @@ generics_declaration = { "<" ~ identifier ~ ( "," ~ identifier )* ~ ">"}
args_list = { arg ~ ( "," ~ arg )* }
arg = { identifier ~ ( ":" ~ "..."? ~ type )? }
expression = { literal }
statement = { call_stmt }
literal = { number_literal }
call_stmt = { call_expr }
call_expr = { call_expr_with_parens | call_expr_no_parens }
call_expr_with_parens = { fqn ~ "(" ~ call_args? ~ ")" }
call_expr_no_parens = { fqn ~ call_args }
call_args = { call_arg ~ ( "," ~ call_arg )* }
call_arg = { expression }
expression = { literal | identifier }
literal = { number_literal | string_literal }
number_literal = { int_literal }
int_literal = { '0'..'9'+ }
WHITESPACE = { " " | "\t" | "\n" | "\r" }
string_literal = ${ "\"" ~ string_inner ~ "\"" }
string_inner = @{ string_char* }
string_char = {
!( "\"" | "\\" ) ~ ANY
| "\\" ~ ( "\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t" )
| "\\" ~ ( "u" ~ ASCII_HEX_DIGIT{4} )
}
WHITESPACE = _{ " " | "\t" | "\n" | "\r" }