summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2023-09-10 19:37:34 +0200
committerMartin Fischer <martin@push-f.com>2023-09-28 10:36:08 +0200
commit852d5c6f2e65a5ab466662ae1c649a0ed25c70a9 (patch)
tree96d6bcdb2f2274f1081a0b6cfbde314f319159a1 /examples
parenta03cea75d9d120a7519be91ec872b143b5d74276 (diff)
break!: move offsets out of Token
Previously the Token enum contained the offsets using the O generic type parameter, which could be a usize if you're tracking offsets or a zero-sized type if you didn't care about offsets. This commit moves all the byte offset and syntax information to a new Trace enum, which has several advantages: * Traces can now easily be stored separately, while the tokens are fed to the tree builder. (The tree builder only has to keep track of which tree nodes originate from which tokens.) * No needless generics for functions that take a token but don't care about offsets (a tree construction implementation is bound to have many of such functions). * The FromIterator<(String, String)> impl for AttributeMap no longer has to specify arbitrary values for the spans and the value_syntax). * The PartialEq implementation of Token is now much more useful (since it no longer includes all the offsets). * The Debug formatting of Token is now more readable (since it no longer includes all the offsets). * Function pointers to functions accepting tokens are possible. (Since function pointer types may not have generic parameters.)
Diffstat (limited to 'examples')
-rw-r--r--examples/spans.rs13
1 files changed, 8 insertions, 5 deletions
diff --git a/examples/spans.rs b/examples/spans.rs
index c1fe23b..b8d5283 100644
--- a/examples/spans.rs
+++ b/examples/spans.rs
@@ -4,14 +4,14 @@ use codespan_reporting::{
term,
term::termcolor::{ColorChoice, StandardStream},
};
-use html5tokenizer::{offset::PosTrackingReader, NaiveParser, Token, TracingEmitter};
+use html5tokenizer::{offset::PosTrackingReader, trace::Trace, NaiveParser, Token, TracingEmitter};
fn main() {
let html = r#"<img src=example.jpg alt="some description">"#;
let parser =
NaiveParser::new_with_emitter(PosTrackingReader::new(html), TracingEmitter::default());
- let Token::StartTag(tag) = parser.flatten().next().unwrap() else {
+ let (Token::StartTag(tag), Trace::StartTag(trace)) = parser.flatten().next().unwrap() else {
panic!()
};
@@ -20,11 +20,14 @@ fn main() {
let mut labels = Vec::new();
- labels.push(Label::primary(file_id, tag.name_span).with_message("tag name"));
+ labels.push(Label::primary(file_id, trace.name_span).with_message("tag name"));
for attr in &tag.attributes {
- labels.push(Label::primary(file_id, attr.name_span()).with_message("attr name"));
- labels.push(Label::primary(file_id, attr.value_span().unwrap()).with_message("attr value"));
+ let attr_trace = &trace.attribute_traces[attr.trace_idx().unwrap()];
+ labels.push(Label::primary(file_id, attr_trace.name_span()).with_message("attr name"));
+ labels.push(
+ Label::primary(file_id, attr_trace.value_span().unwrap()).with_message("attr value"),
+ );
}
let diagnostic = Diagnostic::note().with_labels(labels);