Skip to content

Commit

Permalink
[wast] make constructor of Id public (#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinitus authored Jan 18, 2024
1 parent c998356 commit aaf941d
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions crates/wast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! associated specifically with the wasm text format per se (useful in other
//! contexts too perhaps).

use crate::annotation;
use crate::lexer::Float;
use crate::lexer::{Float, Lexer, TokenKind};
use crate::parser::{Cursor, Parse, Parser, Peek, Result};
use crate::{annotation, Error};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str;
Expand Down Expand Up @@ -58,8 +58,21 @@ pub struct Id<'a> {
}

impl<'a> Id<'a> {
fn new(name: &'a str, span: Span) -> Id<'a> {
Id { name, gen: 0, span }
/// Construct a new identifier from given string.
///
/// Returns an error if the string does not contain a leading `$`, or is not a
/// valid WASM text format identifier.
pub fn new(name: &'a str, span: Span) -> Result<Id<'a>> {
let mut _pos: usize = 0;
let tok = Lexer::new(name).parse(&mut _pos)?;
match tok {
Some(tok) if tok.kind == TokenKind::Id => Ok(Id {
name: tok.id(name),
gen: 0,
span,
}),
_ => Err(Error::parse(span, name, "expected an identifier".into())),
}
}

pub(crate) fn gensym(span: Span, gen: u32) -> Id<'a> {
Expand Down Expand Up @@ -106,7 +119,14 @@ impl<'a> Parse<'a> for Id<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
if let Some((name, rest)) = c.id()? {
return Ok((Id::new(name, c.cur_span()), rest));
return Ok((
Id {
name,
gen: 0,
span: c.cur_span(),
},
rest,
));
}
Err(c.error("expected an identifier"))
})
Expand Down

0 comments on commit aaf941d

Please sign in to comment.