Skip to content

Commit

Permalink
ilex: Make tokens carry an ilex::Context
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Feb 24, 2024
1 parent 8d36068 commit 8d59657
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 67 deletions.
6 changes: 3 additions & 3 deletions ilex/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ impl<'ctx> File<'ctx> {
}

/// Tokenizes the this file according to `spec` and generates a token stream.
pub fn lex<'spec>(
pub fn lex(
self,
spec: &'spec Spec,
spec: &'ctx Spec,
report: &Report,
) -> Result<token::Stream<'spec>, Fatal> {
) -> Result<token::Stream<'ctx>, Fatal> {
rt::lex(self, report, spec)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ilex/src/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ impl Digital<'_> {
&& (exp.is_none()
|| exp.is_some_and(|exp| {
exp.radix() == 10
&& (exp.has_prefix(ctx, "e") || exp.has_prefix(ctx, "E"))
&& (exp.has_prefix("e") || exp.has_prefix("E"))
&& has_ordinary_sign(ctx, &exp)
}))
&& (rule.separator.is_empty()
Expand Down
25 changes: 11 additions & 14 deletions ilex/src/report/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use byteyarn::yarn;
use byteyarn::YarnBox;

use crate::f;
use crate::file::Context;
use crate::file::Spanned;
use crate::plural;
use crate::report::Diagnostic;
Expand Down Expand Up @@ -43,10 +42,10 @@ impl Builtins<'_> {
.report
.error(f!(
"unexpected {} in {}",
found.for_user_diagnostic(self.spec, &self.report.ctx),
found.for_user_diagnostic(self.spec),
unexpected_in
.into()
.for_user_diagnostic(self.spec, &self.report.ctx),
.for_user_diagnostic(self.spec),
))
.at(at)
.reported_at(Location::caller());
Expand Down Expand Up @@ -85,7 +84,7 @@ impl Builtins<'_> {
plural(found.chars().count()),
unexpected_in
.into()
.for_user_diagnostic(self.spec, &self.report.ctx),
.for_user_diagnostic(self.spec),
))
.at(at)
.remark(
Expand All @@ -109,14 +108,14 @@ impl Builtins<'_> {
at: impl Spanned,
) -> Diagnostic {
let expected = expected.into_iter().map(Into::into).collect::<Vec<_>>();
let alts = disjunction_to_string(self.spec, &self.report.ctx, &expected);
let alts = disjunction_to_string(self.spec, &expected);
let found = found.into();

let diagnostic = self
.report
.error(f!(
"expected {alts}, but found {}",
found.for_user_diagnostic(self.spec, &self.report.ctx)
found.for_user_diagnostic(self.spec)
))
.saying(at, f!("expected {alts}"))
.reported_at(Location::caller());
Expand All @@ -140,7 +139,7 @@ impl Builtins<'_> {
.report
.error(f!(
"unexpected closing {}",
found.for_user_diagnostic(self.spec, &self.report.ctx)
found.for_user_diagnostic(self.spec)
))
.saying(at, f!("expected to be opened by `{expected}`"))
.reported_at(Location::caller());
Expand All @@ -165,7 +164,7 @@ impl Builtins<'_> {
.report
.error(f!(
"expected closing `{expected}`, but found {}",
found.for_user_diagnostic(self.spec, &self.report.ctx)
found.for_user_diagnostic(self.spec)
))
.saying(at, f!("expected `{expected}` here"))
.remark(open, "previously opened here")
Expand All @@ -189,7 +188,7 @@ impl Builtins<'_> {
"unexpected non-ASCII characters in {}",
expected
.into()
.for_user_diagnostic(self.spec, &self.report.ctx)
.for_user_diagnostic(self.spec)
))
.at(at)
.reported_at(Location::caller())
Expand Down Expand Up @@ -263,7 +262,7 @@ impl Builtins<'_> {
.report
.error(f!(
"{} out of span",
what.into().for_user_diagnostic(self.spec, &self.report.ctx)
what.into().for_user_diagnostic(self.spec)
))
.at(at)
.note(f!(
Expand Down Expand Up @@ -313,12 +312,11 @@ fn non_printable_note(found: Expected, diagnostic: Diagnostic) -> Diagnostic {

fn disjunction_to_string<'a>(
spec: &'a Spec,
ctx: &'a Context,
lexemes: &'a [Expected],
) -> YarnBox<'a, str> {
let mut names = lexemes
.iter()
.map(|tok| tok.for_user_diagnostic(spec, ctx))
.map(|tok| tok.for_user_diagnostic(spec))
.collect::<Vec<_>>();
names.sort();
names.dedup();
Expand Down Expand Up @@ -424,13 +422,12 @@ impl Expected<'_> {
pub(crate) fn for_user_diagnostic<'a>(
&'a self,
spec: &'a Spec,
ctx: &Context,
) -> YarnBox<'a, str> {
match self {
Self::Literal(lit) => yarn!("`{lit}`"),
Self::Name(name) => name.as_ref().to_box(),
Self::Lexeme(lex) => lex.to_yarn(spec),
Self::Token(tok) => tok.to_yarn(ctx),
Self::Token(tok) => tok.to_yarn(),
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion ilex/src/rt/emit2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ pub fn emit(lexer: &mut Lexer) {
prefix,
suffix,
};
let token = Cursor::fake_token(lexer.spec(), &tok);
let token = Cursor::fake_token(lexer.file(), lexer.spec(), &tok);

// This happens later so we have access to the full spans of
// the digit blocks.
Expand Down Expand Up @@ -747,6 +747,7 @@ pub fn emit(lexer: &mut Lexer) {
if match_.extra > 0 {
let expected = if generated_token {
Expected::Token(token::Cursor::fake_token(
lexer.file(),
lexer.spec(),
lexer.last_token(),
))
Expand Down Expand Up @@ -778,6 +779,7 @@ pub fn emit(lexer: &mut Lexer) {

let expected = if generated_token {
Expected::Token(token::Cursor::fake_token(
lexer.file(),
lexer.spec(),
lexer.last_token(),
))
Expand Down
14 changes: 7 additions & 7 deletions ilex/src/rt/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use super::unicode::is_xid;

/// The lexer state struct, that tracks everything going on during a lexing
/// operation.
pub struct Lexer<'a, 'spec, 'ctx> {
pub struct Lexer<'a, 'ctx> {
report: &'a Report,
spec: &'spec Spec,
spec: &'ctx Spec,
file: File<'ctx>,

cursor: usize,
Expand All @@ -46,9 +46,9 @@ pub struct Closer {
close: Yarn,
}

impl<'a, 'spec, 'ctx> Lexer<'a, 'spec, 'ctx> {
impl<'a, 'ctx> Lexer<'a, 'ctx> {
/// Creates a new lexer.
pub fn new(file: File<'ctx>, report: &'a Report, spec: &'spec Spec) -> Self {
pub fn new(file: File<'ctx>, report: &'a Report, spec: &'ctx Spec) -> Self {
Lexer {
eof: file.span(file.len()..file.len()).intern(file.context()),
cache: Cache::new(&spec.dfa().engine),
Expand Down Expand Up @@ -79,7 +79,7 @@ impl<'a, 'spec, 'ctx> Lexer<'a, 'spec, 'ctx> {
}

/// Returns the spec we're lexing against.
pub fn spec(&self) -> &'spec Spec {
pub fn spec(&self) -> &'ctx Spec {
self.spec
}

Expand Down Expand Up @@ -265,7 +265,7 @@ impl<'a, 'spec, 'ctx> Lexer<'a, 'spec, 'ctx> {
}
}

pub fn finish(mut self) -> token::Stream<'spec> {
pub fn finish(mut self) -> token::Stream<'ctx> {
self.add_token(rt::Token {
kind: rt::Kind::Eof,
span: self.eof,
Expand All @@ -281,6 +281,6 @@ impl<'a, 'spec, 'ctx> Lexer<'a, 'spec, 'ctx> {
.unclosed(open, &close.close, Lexeme::eof(), self.eof());
}

token::Stream { spec: self.spec, toks: self.tokens }
token::Stream { file: self.file, spec: self.spec, toks: self.tokens }
}
}
8 changes: 4 additions & 4 deletions ilex/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ mod dfa;
pub use dfa::compile;
pub use dfa::Dfa;

pub fn lex<'spec>(
file: File,
pub fn lex<'ctx>(
file: File<'ctx>,
report: &Report,
spec: &'spec Spec,
) -> Result<token::Stream<'spec>, Fatal> {
spec: &'ctx Spec,
) -> Result<token::Stream<'ctx>, Fatal> {
let mut lexer = lexer::Lexer::new(file, report, spec);

let mut unexpected_start = None;
Expand Down
Loading

0 comments on commit 8d59657

Please sign in to comment.