1use regen::core::LangBuilder;
2use regen::grammar::{self, Tok};
3use regen::sdk::{
4 ASTParser, CreateParseTree, ParseTreeResultSemantic, TokenBlocks, TokenStream, TokenType,
5};
6use std::fs;
7
8fn main() {
9 let grammar_source = fs::read_to_string("../regen.grammar").unwrap();
13
14 let lex_output = grammar::tokenize(&grammar_source);
20
21 let mut ts = TokenStream::new(&lex_output.tokens, 200);
25
26 let parser = grammar::Parser;
28 let asts = parser.parse_ast_all(&mut ts).unwrap(); let mut outer_tbs = TokenBlocks::new(&grammar_source);
32 lex_output.apply_semantic(&mut outer_tbs);
33 asts.iter()
34 .for_each(|ast| ast.apply_semantic(&mut outer_tbs, &None));
35
36 let mut lang_builder: Box<LangBuilder> = Box::default();
39 for ast in &asts {
40 match ast.parse_pt_with_semantic(outer_tbs, lang_builder) {
42 ParseTreeResultSemantic::Ok { ctx, tbs, .. } => {
43 outer_tbs = tbs;
44 lang_builder = ctx;
45 },
46 ParseTreeResultSemantic::Err { .. } => {
47 unreachable!();
49 }
50 }
51 }
52
53 let code = outer_tbs.get_html(to_prismjs);
56
57 println!("{}", code);
58}
59
60fn to_prismjs(t: Tok) -> String {
61 match t {
62 Tok::TComment => "token comment".to_owned(),
63 Tok::TKeyword => "token keyword".to_owned(),
64 Tok::TIdentifier => "".to_owned(),
65 Tok::TRegExp => "token regex".to_owned(),
66 Tok::TLiteral => "token string".to_owned(),
67 Tok::TSymbol => "token punctuation".to_owned(),
68 Tok::SToken => "token tag".to_owned(),
69 Tok::SVariable => "token attr-name".to_owned(),
70 Tok::SRule => "token class-name".to_owned(),
71 Tok::SSemantic => "token tag".to_owned(),
72 Tok::SHookName => "token function".to_owned(),
73 Tok::SHookType => "token regex".to_owned(),
74 Tok::SContextType => "token tag".to_owned(),
75 Tok::Decor { tag, base } => format!("{} {}", tag, to_prismjs(*base)),
76 _ => t.html_class().unwrap_or_default(),
77 }
78}