Update to Pest grammar for hello world.
This commit is contained in:
parent
db83cb7403
commit
d4fb4680a5
8
src/compiler/mod.rs
Normal file
8
src/compiler/mod.rs
Normal 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!()
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
mod compiler;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
mod util;
|
mod util;
|
||||||
pub mod vm;
|
pub mod vm;
|
||||||
|
@ -1,24 +1,31 @@
|
|||||||
compilation_unit = { SOI ~ namespace? ~ declaration* ~ EOI }
|
compilation_unit = { SOI ~ namespace? ~ declaration* ~ EOI }
|
||||||
namespace = { "ns" ~ fqn }
|
namespace = { "ns" ~ fqn }
|
||||||
|
|
||||||
fqn = { identifier ~ ( "::" ~ fqn )* }
|
fqn = { identifier ~ ( "::" ~ identifier )* }
|
||||||
identifier = @{ identifier_start_char ~ identifier_char* }
|
identifier = @{ identifier_start_char ~ identifier_char* }
|
||||||
identifier_start_char = { 'a'..'z' | 'A'..'Z' | "_" }
|
identifier_start_char = { 'a'..'z' | 'A'..'Z' | "_" }
|
||||||
identifier_char = { 'a'..'z' | 'A'..'Z' | '0'..'9' | "_" }
|
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 = { "int" ~ identifier ~ generics_declaration? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? }
|
||||||
interface_extends_list = { ":" ~ type ~ ( "+" ~ type )* }
|
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* ~ "}" }
|
module = { "mod" ~ identifier ~ "{" ~ declaration* ~ "}" }
|
||||||
|
|
||||||
property = { identifier ~ ":" ~ type }
|
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_equals_body = { "=" ~ expression }
|
||||||
function_body = { "{" ~ "}" }
|
function_body = { "{" ~ statement* ~ "}" }
|
||||||
|
|
||||||
type = { identifier ~ generics_declaration? }
|
type = { identifier ~ generics_declaration? }
|
||||||
generics_declaration = { "<" ~ identifier ~ ( "," ~ identifier )* ~ ">"}
|
generics_declaration = { "<" ~ identifier ~ ( "," ~ identifier )* ~ ">"}
|
||||||
@ -26,12 +33,29 @@ generics_declaration = { "<" ~ identifier ~ ( "," ~ identifier )* ~ ">"}
|
|||||||
args_list = { arg ~ ( "," ~ arg )* }
|
args_list = { arg ~ ( "," ~ arg )* }
|
||||||
arg = { identifier ~ ( ":" ~ "..."? ~ type )? }
|
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 }
|
number_literal = { int_literal }
|
||||||
|
|
||||||
int_literal = { '0'..'9'+ }
|
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" }
|
Loading…
Reference in New Issue
Block a user