Inspiravetion / highlightrs

"A command line utility to turn rust code into syntax highlighted html"
3 stars 0 forks source link

Switch to contextual highlighting #1

Open Inspiravetion opened 9 years ago

Inspiravetion commented 9 years ago

Highlightrs currently uses rustdoc::html::highlight::highlight to do the highlighting but thats just a thin wrapper around the scanner. It can't recognize bad syntax or language constructs. Lets try and switch to creating a trait that prints the html for a type and implement it for each std::ast::* type. The hardest part will be creating the parser and getting it to parse a non-valid program, ie. no imports or unresolved functions;

Functions of interest: CrateConfig::new() is actually Vec::new() syntax::parse::new_parse_sess() -> ParseSess syntax::parse::Parser::new(&'a ParseSess, CrateConfig, Box<Reader + 'a>) -> Parser<'a> syntax::parse::Parser::parse_item_with_outer_attributes(&mut self) -> Option<P<Item>>

iterate through all the items and print html for each one

Inspiravetion commented 9 years ago

This code successfully parses a crate. It will also work on any item. any more granular than that and it will panic.

let handler       = syntax::diagnostic::default_handler(syntax::diagnostic::ColorConfig::Always, None);
let codemap    = syntax::codemap::CodeMap::new();
let filemap       = codemap.new_filemap(name, src);
let sp_handler = syntax::diagnostic::mk_span_handler(handler, codemap);
let reader         = syntax::parse::lexer::StringReader::new(&sp_handler, filemap);

let cfg = Vec::new();
let sess = new_parse_sess();
let mut parser = Parser::new(&sess, cfg, box reader); 
let krate = parser.parse_crate_mod();
println!("{}", krate);