simplify codegen

This commit is contained in:
Aleksey Kladov 2018-07-30 18:02:13 +03:00
parent 7fb7cfc2d1
commit 60e8a845ca
3 changed files with 60 additions and 63 deletions

View file

@ -40,22 +40,6 @@ pub enum SyntaxKind {
NEQ,
MINUS,
THIN_ARROW,
ERROR,
IDENT,
UNDERSCORE,
WHITESPACE,
INT_NUMBER,
FLOAT_NUMBER,
LIFETIME,
CHAR,
BYTE,
STRING,
RAW_STRING,
BYTE_STRING,
RAW_BYTE_STRING,
COMMENT,
DOC_COMMENT,
SHEBANG,
USE_KW,
FN_KW,
STRUCT_KW,
@ -88,6 +72,22 @@ pub enum SyntaxKind {
AUTO_KW,
DEFAULT_KW,
UNION_KW,
ERROR,
IDENT,
UNDERSCORE,
WHITESPACE,
INT_NUMBER,
FLOAT_NUMBER,
LIFETIME,
CHAR,
BYTE,
STRING,
RAW_STRING,
BYTE_STRING,
RAW_BYTE_STRING,
COMMENT,
DOC_COMMENT,
SHEBANG,
FILE,
STRUCT_ITEM,
ENUM_ITEM,
@ -186,22 +186,6 @@ impl SyntaxKind {
NEQ => &SyntaxInfo { name: "NEQ" },
MINUS => &SyntaxInfo { name: "MINUS" },
THIN_ARROW => &SyntaxInfo { name: "THIN_ARROW" },
ERROR => &SyntaxInfo { name: "ERROR" },
IDENT => &SyntaxInfo { name: "IDENT" },
UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" },
WHITESPACE => &SyntaxInfo { name: "WHITESPACE" },
INT_NUMBER => &SyntaxInfo { name: "INT_NUMBER" },
FLOAT_NUMBER => &SyntaxInfo { name: "FLOAT_NUMBER" },
LIFETIME => &SyntaxInfo { name: "LIFETIME" },
CHAR => &SyntaxInfo { name: "CHAR" },
BYTE => &SyntaxInfo { name: "BYTE" },
STRING => &SyntaxInfo { name: "STRING" },
RAW_STRING => &SyntaxInfo { name: "RAW_STRING" },
BYTE_STRING => &SyntaxInfo { name: "BYTE_STRING" },
RAW_BYTE_STRING => &SyntaxInfo { name: "RAW_BYTE_STRING" },
COMMENT => &SyntaxInfo { name: "COMMENT" },
DOC_COMMENT => &SyntaxInfo { name: "DOC_COMMENT" },
SHEBANG => &SyntaxInfo { name: "SHEBANG" },
USE_KW => &SyntaxInfo { name: "USE_KW" },
FN_KW => &SyntaxInfo { name: "FN_KW" },
STRUCT_KW => &SyntaxInfo { name: "STRUCT_KW" },
@ -234,6 +218,22 @@ impl SyntaxKind {
AUTO_KW => &SyntaxInfo { name: "AUTO_KW" },
DEFAULT_KW => &SyntaxInfo { name: "DEFAULT_KW" },
UNION_KW => &SyntaxInfo { name: "UNION_KW" },
ERROR => &SyntaxInfo { name: "ERROR" },
IDENT => &SyntaxInfo { name: "IDENT" },
UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" },
WHITESPACE => &SyntaxInfo { name: "WHITESPACE" },
INT_NUMBER => &SyntaxInfo { name: "INT_NUMBER" },
FLOAT_NUMBER => &SyntaxInfo { name: "FLOAT_NUMBER" },
LIFETIME => &SyntaxInfo { name: "LIFETIME" },
CHAR => &SyntaxInfo { name: "CHAR" },
BYTE => &SyntaxInfo { name: "BYTE" },
STRING => &SyntaxInfo { name: "STRING" },
RAW_STRING => &SyntaxInfo { name: "RAW_STRING" },
BYTE_STRING => &SyntaxInfo { name: "BYTE_STRING" },
RAW_BYTE_STRING => &SyntaxInfo { name: "RAW_BYTE_STRING" },
COMMENT => &SyntaxInfo { name: "COMMENT" },
DOC_COMMENT => &SyntaxInfo { name: "DOC_COMMENT" },
SHEBANG => &SyntaxInfo { name: "SHEBANG" },
FILE => &SyntaxInfo { name: "FILE" },
STRUCT_ITEM => &SyntaxInfo { name: "STRUCT_ITEM" },
ENUM_ITEM => &SyntaxInfo { name: "ENUM_ITEM" },
@ -285,7 +285,6 @@ impl SyntaxKind {
LIFETIME_PARAM => &SyntaxInfo { name: "LIFETIME_PARAM" },
TYPE_PARAM_LIST => &SyntaxInfo { name: "TYPE_PARAM_LIST" },
TYPE_ARG_LIST => &SyntaxInfo { name: "TYPE_ARG_LIST" },
TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" },
EOF => &SyntaxInfo { name: "EOF" },
}

View file

@ -5,23 +5,14 @@ use super::SyntaxInfo;
/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SyntaxKind {
{%- for t in single_byte_tokens %}
{%- for t in concat(a=single_byte_tokens, b=multi_byte_tokens) %}
{{t.1}},
{%- endfor -%}
{% for t in multi_byte_tokens %}
{{t.1}},
{% for kw in concat(a=keywords, b=contextual_keywords) %}
{{kw | upper}}_KW,
{%- endfor -%}
{% for t in tokens %}
{% for t in concat(a=tokens, b=nodes) %}
{{t}},
{%- endfor -%}
{% for kw in keywords %}
{{kw | upper}}_KW,
{%- endfor -%}
{% for kw in contextual_keywords %}
{{kw | upper}}_KW,
{%- endfor -%}
{% for node in nodes %}
{{node}},
{%- endfor %}
// Technical SyntaxKinds: they appear temporally during parsing,
// but never end up in the final tree
@ -35,25 +26,15 @@ use self::SyntaxKind::*;
impl SyntaxKind {
pub(crate) fn info(self) -> &'static SyntaxInfo {
match self {
{%- for t in single_byte_tokens %}
{%- for t in concat(a=single_byte_tokens, b=multi_byte_tokens) %}
{{t.1}} => &SyntaxInfo { name: "{{t.1}}" },
{%- endfor -%}
{% for t in multi_byte_tokens %}
{{t.1}} => &SyntaxInfo { name: "{{t.1}}" },
{% for kw in concat(a=keywords, b=contextual_keywords) %}
{{kw | upper}}_KW => &SyntaxInfo { name: "{{kw | upper}}_KW" },
{%- endfor -%}
{% for t in tokens %}
{% for t in concat(a=tokens, b=nodes) %}
{{t}} => &SyntaxInfo { name: "{{t}}" },
{%- endfor -%}
{% for kw in keywords %}
{{kw | upper}}_KW => &SyntaxInfo { name: "{{kw | upper}}_KW" },
{%- endfor -%}
{% for kw in contextual_keywords %}
{{kw | upper}}_KW => &SyntaxInfo { name: "{{kw | upper}}_KW" },
{%- endfor -%}
{% for node in nodes %}
{{node}} => &SyntaxInfo { name: "{{node}}" },
{%- endfor %}
TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" },
EOF => &SyntaxInfo { name: "EOF" },
}

View file

@ -6,7 +6,7 @@ extern crate tera;
extern crate walkdir;
extern crate tools;
use std::{collections::HashSet, fs, path::Path};
use std::{collections::{HashSet, HashMap}, fs, path::Path};
use clap::{App, Arg, SubCommand};
use tools::{collect_tests, Test};
@ -63,9 +63,26 @@ fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
fn get_kinds() -> Result<String> {
let grammar = grammar()?;
let template = fs::read_to_string(SYNTAX_KINDS_TEMPLATE)?;
let ret = tera::Tera::one_off(&template, &grammar, false)
let mut tera = tera::Tera::default();
tera.add_raw_template("grammar", &template)
.map_err(|e| format_err!("template error: {:?}", e))?;
Ok(ret)
tera.register_global_function("concat", Box::new(concat));
let ret = tera.render("grammar", &grammar)
.map_err(|e| format_err!("template error: {:?}", e))?;
return Ok(ret);
fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
let mut elements = Vec::new();
for &key in ["a", "b", "c"].iter() {
let val = match args.get(key) {
Some(val) => val,
None => continue,
};
let val = val.as_array().unwrap();
elements.extend(val.iter().cloned());
}
Ok(tera::Value::Array(elements))
}
}
fn grammar() -> Result<ron::value::Value> {