use regen::core::LangBuilder;
use regen::grammar::{self, Tok};
use regen::sdk::{
ASTParser, CreateParseTree, ParseTreeResultSemantic, TokenBlocks, TokenStream, TokenType,
};
use std::fs;
fn main() {
let grammar_source = fs::read_to_string("../regen.grammar").unwrap();
let lex_output = grammar::tokenize(&grammar_source);
let mut ts = TokenStream::new(&lex_output.tokens, 200);
let parser = grammar::Parser;
let asts = parser.parse_ast_all(&mut ts).unwrap(); let mut outer_tbs = TokenBlocks::new(&grammar_source);
lex_output.apply_semantic(&mut outer_tbs);
asts.iter()
.for_each(|ast| ast.apply_semantic(&mut outer_tbs, &None));
let mut lang_builder: Box<LangBuilder> = Box::default();
for ast in &asts {
match ast.parse_pt_with_semantic(outer_tbs, lang_builder) {
ParseTreeResultSemantic::Ok { ctx, tbs, .. } => {
outer_tbs = tbs;
lang_builder = ctx;
},
ParseTreeResultSemantic::Err { .. } => {
unreachable!();
}
}
}
let code = outer_tbs.get_html(to_prismjs);
println!("{}", code);
}
fn to_prismjs(t: Tok) -> String {
match t {
Tok::TComment => "token comment".to_owned(),
Tok::TKeyword => "token keyword".to_owned(),
Tok::TIdentifier => "".to_owned(),
Tok::TRegExp => "token regex".to_owned(),
Tok::TLiteral => "token string".to_owned(),
Tok::TSymbol => "token punctuation".to_owned(),
Tok::SToken => "token tag".to_owned(),
Tok::SVariable => "token attr-name".to_owned(),
Tok::SRule => "token class-name".to_owned(),
Tok::SSemantic => "token tag".to_owned(),
Tok::SHookName => "token function".to_owned(),
Tok::SHookType => "token regex".to_owned(),
Tok::SContextType => "token tag".to_owned(),
Tok::Decor { tag, base } => format!("{} {}", tag, to_prismjs(*base)),
_ => t.html_class().unwrap_or_default(),
}
}