pub enum Token {
Boolean(BooleanLiteral),
EoF,
Ident(Ident),
Keyword(Keyword),
Null,
Numeric(Number),
Punct(Punct),
String(StringLit),
RegEx(RegEx),
Template(Template),
Comment(Comment),
}
The representation of a single JS token
An identifier this will be either a variable name
or a function/method name
A word that has been reserved to not be used as an identifier
A number, this includes integers (1
), decimals (0.1
),
hex (0x8f
), binary (0b010011010
), and octal (0o273
)
A punctuation mark, this includes all mathematical operators
logical operators and general syntax punctuation
A string literal, either double or single quoted, the associated
value will be the unquoted string
A regular expression literal.
let regex = /[a-zA-Z]+/g;
The string parts of a template string
let js = "`Things and stuff times ${10} equals ${100000000}... i think`";
let mut s = Scanner::new(js);
assert_eq!(s.next().unwrap().token,
Token::template_head("Things and stuff times "));
assert_eq!(s.next().unwrap().token,
Token::numeric("10"));
assert_eq!(s.next().unwrap().token,
Token::template_middle(" equals "));
assert_eq!(s.next().unwrap().token,
Token::numeric("100000000"));
assert_eq!(s.next().unwrap().token,
Token::template_tail("... i think"));
A comment, the associated value will contain the raw comment
This will capture both inline comments // I am an inline comment
and multi-line comments
/*multi lines
* comments
*/
Create and instance of Token::Ident from a &str
Create and instance of Token::Keyword from a &str
panics if the argument isn't a valid js keyword
Create and instance of Token::Numeric from a &str
Create and instance of Token::Punct from a &str
panics if the augment isn't valid js punctuation
Create and instance of Token::String from a &str wrapped in double quotes
Create an instance of Token::String from a &str wrapped in single quotes
Create an instance of Token::RegEx from a &str and an Option
Creates an instance of Token::Template with a template string that has
no substitutions
var noSub = `template string with no subs`;
Creates an instance of Token::Template for a template head
let t = `head ${true} middle ${false} tail ${false}`;
Creates an instance of a Token::Template for a template middle
let t = `head ${false} middle ${true} tail ${false}`;
Creates an instance of a Token::Template for a template tail
let t = `head ${false} middle ${false} tail ${true}`;
Creates an instance of a Token::Comment for a comment string and a flag
if this comment should be treated as a multi line comment
note, this will not generate HTML-style comments
let single_js = "//I am a comment";
let multi_js = "/*I am a multi-line comment*/";
let mut s = Scanner::new(single_js);
let single_scanner = s.next().expect("unable to parse single line comment");
let single = Token::comment("I am a comment", false);
assert_eq!(single, single_scanner.token);
s = Scanner::new(multi_js);
let multi_scanner = s.next().expect("Unable to parse multi-line comment");
let multi = Token::comment("I am a multi-line comment", true);
assert_eq!(multi, multi_scanner.token);
Converts the given value to a String
. Read more
Performs copy-assignment from source
. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Formats the value using the given formatter. Read more
Converts the given value to a String
. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static