// Syntax of this software // It uses Rust-like and lalrpop-like syntax. // // Copyright (c) 2020 Naoki Kaneko (a.k.a. "puripuri2100") // https://github.com/puripuri2100/llmaker // // Give the file to read as a string. "use super::lexer;" "use super::types;" // Grammar starts here. grammar; // Give tokens' type as a string. extern { enum "lexer::Token" { Tok_EOF => "(lexer::TokenKind::EOF , _)", Tok_GRAMMAR => "(lexer::TokenKind::GRAMMAR , _)", Tok_EXTERN => "(lexer::TokenKind::EXTERN , _)", Tok_ENUM => "(lexer::TokenKind::ENUM , _)", Tok_PUB => "(lexer::TokenKind::PUB , _)", Tok_VAR => "(lexer::TokenKind::VAR (_), _)", Tok_CONSTRUCTOR => "(lexer::TokenKind::CONSTRUCTOR (_), _)", Tok_LCURLYBRACES => "(lexer::TokenKind::LCURLYBRACES , _)", Tok_RCURLYBRACES => "(lexer::TokenKind::RCURLYBRACES , _)", Tok_EQ => "(lexer::TokenKind::EQ , _)", Tok_COMMA => "(lexer::TokenKind::COMMA , _)", Tok_SEMICOLON => "(lexer::TokenKind::SEMICOLON , _)", Tok_COLON => "(lexer::TokenKind::COLON , _)", Tok_LBRACES => "(lexer::TokenKind::LBRACES , _)", Tok_RBRACES => "(lexer::TokenKind::RBRACES , _)", Tok_ARROW => "(lexer::TokenKind::ARROW , _)", Tok_STR => "(lexer::TokenKind::STR (_), _)", } } // Give the function name lowercase. // Add "pub" to main function. // Give function's type as a string. // Give code as a string. pub main: "types::Term" = { <_eof: Tok_EOF> => { "let mut v = head; v.reverse(); (v, setting, body)" }, }; head: "types::Head" = { => { "let mut tail_v = tail; let (stok, rng) = tok; let s = lexer::get_string(stok).unwrap(); tail_v.push((rng, s)); tail_v" }, => {"Vec::new()"}, }; head_tail: "types::Head" = { => { "let mut tail_v = tail; let (stok, rng) = head; let s = lexer::get_string(stok).unwrap(); tail_v.push((rng, s)); tail_v" }, => {"Vec::new()"}, }; gr: "()" = { => {"()"}, }; setting: "types::Setting" = { => { "types" }, }; types: "types::Setting" = { => { "let (stok, _) = nametok; let s = lexer::get_string(stok).unwrap(); let mut settokens = settokens_rev; settokens.reverse(); (s, settokens)" }, }; settokens: "Vec<(types::Range, String, types::TypeStr)>" = { => { "let mut v = settokens; v.push(settoken); v" }, => {"Vec::new()"}, }; settokens_sub: "Vec<(types::Range, String, types::TypeStr)>" = { => {"tail"}, => {"Vec::new()"}, }; settokens_sub_sub: "Vec<(types::Range, String, types::TypeStr)>" = { => { "let mut v = settokens; v.push(settoken); v" }, => {"Vec::new()"}, }; settoken: "(types::Range, String, types::TypeStr)" = { => { "let (v1tok, rng1) = name; let v1 = lexer::get_string(v1tok).unwrap(); let (v2tok, rng2) = typestr; let v2 = lexer::get_string(v2tok).unwrap(); (types::Range::unite(rng1, rng2), v1, v2)" }, }; body: "Vec" = { => { "let mut v = bnflst; v.reverse(); v" }, }; bnflst: "Vec" = { => { "let mut v = bnflst; v.push(bnf); v" }, => {"Vec::new()"}, }; bnflst_sub: "Vec" = { => {"tail"}, => {"Vec::new()"}, }; bnflst_sub_sub: "Vec" = { => { "let mut v = bnflst; v.push(bnf); v" }, => {"Vec::new()"}, }; bnf: "types::Bnf" = { => { "let (nametok, rng1) = fnname; let name = lexer::get_string(nametok).unwrap(); let (stok, _) = typestr; let s = lexer::get_string(stok).unwrap(); let (_, rng2) = v5; let rng = types::Range::unite(rng1, rng2); types::Bnf::Pub(rng, name, s, bnf_code_lst)" }, => { "let (nametok, rng1) = fnname; let name = lexer::get_string(nametok).unwrap(); let (stok, _) = typestr; let s = lexer::get_string(stok).unwrap(); let (_, rng2) = v5; let rng = types::Range::unite(rng1, rng2); let mut bnf_code_lst = bnf_code_lst_rev; bnf_code_lst.reverse(); types::Bnf::NonPub(rng, name, s, bnf_code_lst)" }, }; bnf_code_lst: "Vec" = { => { "let mut v = bnf_code_lst; v.push(bnf_code); v" }, => {"Vec::new()"}, }; bnf_code_lst_sub: "Vec" = { => {"tail"}, => {"Vec::new()"}, }; bnf_code_lst_sub_sub: "Vec" = { => { "let mut v = bnf_code_lst; v.push(bnf_code); v" }, => {"Vec::new()"}, }; bnf_code: "types::Code" = { => { "let (codetok, _) = code; let codestr = lexer::get_string(codetok).unwrap(); let mut v = fn_or_tokens; v.reverse(); (v, codestr)" }, => { "let (codetok, _) = code; let codestr = lexer::get_string(codetok).unwrap(); let mut v = Vec::new(); v.reverse(); (v, codestr)" }, }; fn_or_token_lst: "Vec<(String, types::FnOrToken)>" = { => { "let mut v = fs; v.push(f); v" }, => {"Vec::new()"}, }; fn_or_token_lst_sub: "Vec<(String, types::FnOrToken)>" = { => { "let mut v = fs; v.push(f); v" }, => {"Vec::new()"}, }; fn_or_token: "(String, types::FnOrToken)" = { => { "let (nametok, _) = name; let namestr = lexer::get_string(nametok).unwrap(); (namestr, tail)" } }; fn_or_token_sub: "types::FnOrToken" = { => { "let (fnnametok, _) = fnname; let fnnamestr = lexer::get_string(fnnametok).unwrap(); types::FnOrToken::Function(fnnamestr)" }, => { "let (toknametok, _) = tokname; let toknamestr = lexer::get_string(toknametok).unwrap(); types::FnOrToken::Token(toknamestr)" }, };