use std::collections::BTreeMap;
use super::value::{Function, Object, Value, Variable};
use super::{
Attribute, AttributeType, Expectation, JsString, Location, Meta, ParseError, Parsed,
ParsedType, Position, RangeError, SyntaxError,
};
pub(crate) fn build_message(expected: &[Expectation], found: Option<&JsString>) -> JsString {
fn describe_expectation(expectation: &Expectation) -> Vec<u16> {
match expectation {
Expectation::Literal { text, .. } => {
let mut out = units("\"");
out.extend(literal_escape(&units(text)));
out.extend(units("\""));
out
}
Expectation::Class {
parts, inverted, ..
} => {
let escaped_parts: Vec<Vec<u16>> = parts
.iter()
.map(|part| match part {
super::ClassPart::Range(from, to) => {
let mut out = class_escape(&units(from));
out.extend(units("-"));
out.extend(class_escape(&units(to)));
out
}
super::ClassPart::Char(c) => class_escape(&units(c)),
})
.collect();
let mut out = units("[");
if *inverted {
out.extend(units("^"));
}
out.extend(escaped_parts.join(&(',' as u16)));
out.extend(units("]"));
out
}
Expectation::Any => units("any character"),
Expectation::End => units("end of input"),
Expectation::Other { description } => units(description),
Expectation::Not(expected) => {
let mut out = units("not ");
out.extend(describe_expectation(expected));
out
}
}
}
fn hex(ch: u16) -> Vec<u16> {
units(&format!("{ch:X}"))
}
fn replace(
s: Vec<u16>,
matches: impl Fn(u16) -> bool,
with: impl Fn(u16) -> Vec<u16>,
) -> Vec<u16> {
let mut out = Vec::with_capacity(s.len());
for ch in s {
if matches(ch) {
out.extend(with(ch));
} else {
out.push(ch);
}
}
out
}
fn literal_escape(s: &[u16]) -> Vec<u16> {
let s = replace(s.to_vec(), |c| c == 0x5C, |_| units("\\\\")); let s = replace(s, |c| c == 0x22, |_| units("\\\"")); let s = replace(s, |c| c == 0x00, |_| units("\\0")); let s = replace(s, |c| c == 0x09, |_| units("\\t")); let s = replace(s, |c| c == 0x0A, |_| units("\\n")); let s = replace(s, |c| c == 0x0D, |_| units("\\r")); let s = replace(s, |c| c <= 0x0F, |ch| [units("\\x0"), hex(ch)].concat()); replace(
s,
|c| (0x10..=0x1F).contains(&c) || (0x7F..=0x9F).contains(&c),
|ch| [units("\\x"), hex(ch)].concat(),
) }
fn class_escape(s: &[u16]) -> Vec<u16> {
let s = replace(s.to_vec(), |c| c == 0x5C, |_| units("\\\\")); let s = replace(s, |c| c == 0x5D, |_| units("\\]")); let s = replace(s, |c| c == 0x5E, |_| units("\\^")); let s = replace(s, |c| c == 0x2D, |_| units("\\-")); let s = replace(s, |c| c == 0x00, |_| units("\\0")); let s = replace(s, |c| c == 0x09, |_| units("\\t")); let s = replace(s, |c| c == 0x0A, |_| units("\\n")); let s = replace(s, |c| c == 0x0D, |_| units("\\r")); let s = replace(s, |c| c <= 0x0F, |ch| [units("\\x0"), hex(ch)].concat()); replace(
s,
|c| (0x10..=0x1F).contains(&c) || (0x7F..=0x9F).contains(&c),
|ch| [units("\\x"), hex(ch)].concat(),
) }
fn describe_expected(expected: &[Expectation]) -> Vec<u16> {
let mut descriptions: Vec<Vec<u16>> = expected.iter().map(describe_expectation).collect();
descriptions.sort();
if !descriptions.is_empty() {
let mut j = 1;
for i in 1..descriptions.len() {
if descriptions[i - 1] != descriptions[i] {
descriptions[j] = descriptions[i].clone();
j += 1;
}
}
descriptions.truncate(j);
}
match descriptions.len() {
1 => descriptions[0].clone(),
2 => [
descriptions[0].clone(),
units(" or "),
descriptions[1].clone(),
]
.concat(),
n => {
let mut out = descriptions[..n.saturating_sub(1)].join(&units(", ")[..]);
out.extend(units(", or "));
match descriptions.last() {
Some(last) => out.extend(last),
None => out.extend(units("undefined")),
}
out
}
}
}
fn describe_found(found: Option<&JsString>) -> Vec<u16> {
match found {
Some(found) if !found.0.is_empty() => {
[units("\""), literal_escape(&found.0), units("\"")].concat()
}
_ => units("end of input"),
}
}
JsString(
[
units("Expected "),
describe_expected(expected),
units(" but "),
describe_found(found),
units(" found."),
]
.concat(),
)
}
fn units(s: &str) -> Vec<u16> {
s.encode_utf16().collect()
}
const PEG_C0: u16 = b'/' as u16;
static PEG_E0: Expectation = Expectation::Literal {
text: "/",
ignore_case: false,
};
static PEG_E1: Expectation = Expectation::Other {
description: "tag name",
};
static PEG_E2: Expectation = Expectation::Other {
description: "class",
};
static PEG_E3: Expectation = Expectation::Other { description: "id" };
static PEG_E4: Expectation = Expectation::Literal {
text: "=",
ignore_case: false,
};
static PEG_E5: Expectation = Expectation::Literal {
text: "(",
ignore_case: false,
};
static PEG_E6: Expectation = Expectation::Literal {
text: ")",
ignore_case: false,
};
static PEG_E7: Expectation = Expectation::Literal {
text: ",",
ignore_case: false,
};
static PEG_E8: Expectation = Expectation::Other {
description: "variable",
};
static PEG_E9: Expectation = Expectation::Other {
description: "null",
};
static PEG_E10: Expectation = Expectation::Other {
description: "boolean",
};
static PEG_E11: Expectation = Expectation::Literal {
text: "[",
ignore_case: false,
};
static PEG_E12: Expectation = Expectation::Literal {
text: "]",
ignore_case: false,
};
static PEG_E13: Expectation = Expectation::Literal {
text: "{",
ignore_case: false,
};
static PEG_E14: Expectation = Expectation::Literal {
text: "}",
ignore_case: false,
};
static PEG_E15: Expectation = Expectation::Literal {
text: ":",
ignore_case: false,
};
static PEG_E16: Expectation = Expectation::Other {
description: "number",
};
static PEG_E17: Expectation = Expectation::Other {
description: "string",
};
static PEG_E18: Expectation = Expectation::Other {
description: "identifier",
};
static PEG_E19: Expectation = Expectation::Other {
description: "whitespace",
};
static PEG_END: Expectation = Expectation::End;
const PEG_C9: &[u16] = &[b'n' as u16, b'u' as u16, b'l' as u16, b'l' as u16];
const PEG_C10: &[u16] = &[b't' as u16, b'r' as u16, b'u' as u16, b'e' as u16];
const PEG_C11: &[u16] = &[
b'f' as u16,
b'a' as u16,
b'l' as u16,
b's' as u16,
b'e' as u16,
];
struct Expected {
pos: usize,
variants: Vec<&'static Expectation>,
}
#[derive(Clone, Copy)]
struct PosDetails {
line: usize,
column: usize,
}
pub const MAX_NESTING: usize = 300;
pub mod cost {
use crate::depth::{extra, per_level};
pub const ARRAY: u64 = per_level(4_074);
pub const ARRAY_TAIL: u64 = extra(4_074, 2_401);
pub const HASH: u64 = per_level(2_401);
pub const HASH_TAIL: u64 = extra(2_401, 1_766);
pub const CALL: u64 = per_level(2_353);
pub const PARAMETER_TAIL: u64 = extra(2_353, 1_740);
}
struct Parser<'a> {
input: &'a [u16],
curr_pos: usize,
saved_pos: usize,
pos_details_cache: BTreeMap<usize, PosDetails>,
expected: Vec<Expected>,
silent_fails: u32,
depth: usize,
too_deep: bool,
}
pub(crate) fn peg_parse(input: &[u16]) -> Result<Parsed, ParseError> {
let mut p = Parser {
input,
curr_pos: 0,
saved_pos: 0,
pos_details_cache: BTreeMap::from([(0, PosDetails { line: 1, column: 1 })]),
expected: Vec::new(),
silent_fails: 0,
depth: 0,
too_deep: false,
};
p.peg_begin();
let peg_result = p.parse_top();
if p.too_deep {
return Err(ParseError::Range(RangeError(
"Maximum call stack size exceeded".to_owned(),
)));
}
match peg_result {
Some(result) if p.curr_pos == input.len() => Ok(result),
peg_result => {
if peg_result.is_some() && p.curr_pos < input.len() {
p.peg_expect(&PEG_END);
}
Err(ParseError::Syntax(p.peg_build_error()))
}
}
}
impl Parser<'_> {
fn char_code_at(&self, pos: usize) -> Option<u16> {
self.input.get(pos).copied()
}
fn substr_is(&self, pos: usize, lit: &[u16]) -> bool {
self.input.get(pos..pos + lit.len()) == Some(lit)
}
fn substring(&self, start: usize, end: usize) -> String {
String::from_utf16_lossy(&self.input[start..end])
}
fn text(&self) -> String {
self.substring(self.saved_pos, self.curr_pos)
}
fn rule_expects(&mut self, expected: &'static Expectation) {
if self.silent_fails == 0 {
self.peg_expect(expected);
}
}
fn peg_compute_pos_details(&mut self, pos: usize) -> PosDetails {
if let Some(&details) = self.pos_details_cache.get(&pos) {
details
} else {
let (mut p, mut details) = self
.pos_details_cache
.range(..pos)
.next_back()
.map(|(&p, &details)| (p, details))
.expect("peg$posDetailsCache[0] is set");
while p < pos {
if self.char_code_at(p) == Some(10) {
details.line += 1;
details.column = 1;
} else {
details.column += 1;
}
p += 1;
}
self.pos_details_cache.insert(pos, details);
details
}
}
fn peg_compute_location(&mut self, start_pos: usize, end_pos: usize) -> Location {
let start_pos_details = self.peg_compute_pos_details(start_pos);
let start = Position {
offset: start_pos,
line: start_pos_details.line,
column: start_pos_details.column,
};
let end_pos_details = self.peg_compute_pos_details(end_pos);
let end = Position {
offset: end_pos,
line: end_pos_details.line,
column: end_pos_details.column,
};
Location { start, end }
}
fn peg_begin(&mut self) {
self.expected.push(Expected {
pos: self.curr_pos,
variants: Vec::new(),
});
}
fn peg_expect(&mut self, expected: &'static Expectation) {
let curr_pos = self.curr_pos;
let top = self.expected.last_mut().expect("peg$begin pushed an entry");
if curr_pos < top.pos {
return;
}
if curr_pos > top.pos {
top.pos = curr_pos;
top.variants = Vec::new();
}
top.variants.push(expected);
}
fn peg_build_structured_error(
expected: Vec<Expectation>,
found: Option<JsString>,
location: Location,
) -> SyntaxError {
SyntaxError {
message: build_message(&expected, found.as_ref()),
expected,
found,
location,
}
}
fn peg_build_error(&mut self) -> SyntaxError {
let expected = &self.expected[0];
let fail_pos = expected.pos;
let variants: Vec<Expectation> = expected.variants.iter().map(|&e| e.clone()).collect();
let found = if fail_pos < self.input.len() {
Some(JsString(vec![self.input[fail_pos]]))
} else {
None
};
let location = if fail_pos < self.input.len() {
self.peg_compute_location(fail_pos, fail_pos + 1)
} else {
self.peg_compute_location(fail_pos, fail_pos)
};
Self::peg_build_structured_error(variants, found, location)
}
fn parse_top(&mut self) -> Option<Parsed> {
let mut s0;
s0 = self.parse_top_level_value();
if s0.is_none() {
s0 = self.parse_annotation();
if s0.is_none() {
s0 = self.parse_tag_open();
if s0.is_none() {
s0 = self.parse_tag_close();
}
}
}
s0
}
fn parse_top_level_value(&mut self) -> Option<Parsed> {
let s0 = self.curr_pos;
let mut s1 = self.parse_variable();
if s1.is_none() {
s1 = self.parse_function();
}
match s1 {
Some(s1) => {
self.saved_pos = s0;
Some(peg_f0(s1))
}
None => None,
}
}
fn parse_annotation(&mut self) -> Option<Parsed> {
let s0 = self.curr_pos;
let s1 = self.parse_tag_attributes();
if let Some(s1) = s1 {
let mut s3 = self.parse__();
while s3.is_some() {
s3 = self.parse__();
}
self.saved_pos = s0;
Some(peg_f1(s1))
} else {
self.curr_pos = s0;
None
}
}
fn parse_tag_open(&mut self) -> Option<Parsed> {
let s0 = self.curr_pos;
let s1 = self.parse_tag_name();
if let Some(s1) = s1 {
let mut s3 = self.parse__();
while s3.is_some() {
s3 = self.parse__();
}
let s3 = self.curr_pos;
let s4 = self.parse_value();
let s3: Option<Value> = if let Some(s4) = s4 {
let _s5 = self.parse__(); self.saved_pos = s3;
Some(peg_f2(&s1, s4))
} else {
self.curr_pos = s3;
None
};
let s4 = self.parse_tag_attributes(); let mut s6 = self.parse__();
while s6.is_some() {
s6 = self.parse__();
}
self.rule_expects(&PEG_E0);
let s6 = if self.char_code_at(self.curr_pos) == Some(47) {
self.curr_pos += 1;
Some(PEG_C0)
} else {
None
}; self.saved_pos = s0;
Some(peg_f3(s1, s3, s4, s6))
} else {
self.curr_pos = s0;
None
}
}
fn parse_tag_close(&mut self) -> Option<Parsed> {
let s0 = self.curr_pos;
self.rule_expects(&PEG_E0);
let s1 = if self.char_code_at(self.curr_pos) == Some(47) {
self.curr_pos += 1;
Some(PEG_C0)
} else {
None
};
if s1.is_some() {
let s2 = self.parse_tag_name();
if let Some(s2) = s2 {
self.saved_pos = s0;
Some(peg_f4(s2))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_tag_name(&mut self) -> Option<String> {
self.rule_expects(&PEG_E1);
self.silent_fails += 1;
let s0 = self.parse_identifier();
self.silent_fails -= 1;
s0
}
fn parse_tag_attributes(&mut self) -> Option<Vec<Attribute>> {
let s0 = self.curr_pos;
let s1 = self.parse_tag_attributes_item();
if let Some(s1) = s1 {
let mut s2 = Vec::new();
let mut s3 = self.parse_tag_attributes_tail();
while let Some(item) = s3 {
s2.push(item);
s3 = self.parse_tag_attributes_tail();
}
self.saved_pos = s0;
Some(peg_f5(s1, s2))
} else {
self.curr_pos = s0;
None
}
}
fn parse_tag_attributes_tail(&mut self) -> Option<Attribute> {
let s0 = self.curr_pos;
let mut s2 = self.parse__();
let s1 = if s2.is_some() {
while s2.is_some() {
s2 = self.parse__();
}
Some(())
} else {
None
};
if s1.is_some() {
let s2 = self.parse_tag_attributes_item();
if let Some(s2) = s2 {
self.saved_pos = s0;
Some(peg_f6(s2))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_tag_attributes_item(&mut self) -> Option<Attribute> {
let mut s0 = self.curr_pos;
let s1 = self.parse_tag_shortcut_id();
let mut result = if let Some(s1) = s1 {
self.saved_pos = s0;
Some(peg_f7(s1))
} else {
None
};
if result.is_none() {
s0 = self.curr_pos;
let s1 = self.parse_tag_shortcut_class();
result = if let Some(s1) = s1 {
self.saved_pos = s0;
Some(peg_f8(s1))
} else {
None
};
if result.is_none() {
s0 = self.curr_pos;
let s1 = self.parse_tag_attribute();
result = if let Some(s1) = s1 {
self.saved_pos = s0;
Some(peg_f9(s1))
} else {
None
};
}
}
result
}
fn parse_tag_shortcut_class(&mut self) -> Option<Attribute> {
self.rule_expects(&PEG_E2);
self.silent_fails += 1;
let s0 = self.curr_pos;
let s1 = if self.char_code_at(self.curr_pos) == Some(46) {
self.curr_pos += 1;
Some(())
} else {
None
};
let result = if s1.is_some() {
let s2 = self.parse_identifier();
if let Some(s2) = s2 {
self.saved_pos = s0;
Some(peg_f10(s2))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
};
self.silent_fails -= 1;
result
}
fn parse_tag_shortcut_id(&mut self) -> Option<Attribute> {
self.rule_expects(&PEG_E3);
self.silent_fails += 1;
let s0 = self.curr_pos;
let s1 = if self.char_code_at(self.curr_pos) == Some(35) {
self.curr_pos += 1;
Some(())
} else {
None
};
let result = if s1.is_some() {
let s2 = self.parse_identifier();
if let Some(s2) = s2 {
self.saved_pos = s0;
Some(peg_f11(s2))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
};
self.silent_fails -= 1;
result
}
fn parse_tag_attribute(&mut self) -> Option<Attribute> {
let s0 = self.curr_pos;
let s1 = self.parse_identifier();
if let Some(s1) = s1 {
self.rule_expects(&PEG_E4);
let s2 = if self.char_code_at(self.curr_pos) == Some(61) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s2.is_some() {
let s3 = self.parse_value();
if let Some(s3) = s3 {
self.saved_pos = s0;
Some(peg_f12(s1, s3))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_function(&mut self) -> Option<Value> {
let s0 = self.curr_pos;
let s1 = self.parse_identifier();
if let Some(s1) = s1 {
self.rule_expects(&PEG_E5);
let s2 = if self.char_code_at(self.curr_pos) == Some(40) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s2.is_some() {
let Some(_level) = self.level(cost::CALL) else {
self.curr_pos = s0;
return None;
};
let mut s4 = self.parse__();
while s4.is_some() {
s4 = self.parse__();
}
let s4 = self.curr_pos;
let s5 = self.parse_function_parameter(); let mut s6 = Vec::new();
let mut s7 = self.parse_function_parameter_tail();
while let Some(param) = s7 {
s6.push(param);
s7 = self.parse_function_parameter_tail();
}
self.saved_pos = s4;
let s4 = peg_f13(&s1, s5, s6);
self.rule_expects(&PEG_E6);
let s5 = if self.char_code_at(self.curr_pos) == Some(41) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s5.is_some() {
self.saved_pos = s0;
Some(peg_f14(s1, s4))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_function_parameter(&mut self) -> Option<Param> {
let s0 = self.curr_pos;
let s1 = self.curr_pos;
let s2 = self.parse_identifier();
let s1: Option<String> = if let Some(s2) = s2 {
self.rule_expects(&PEG_E4);
let s3 = if self.char_code_at(self.curr_pos) == Some(61) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s3.is_some() {
self.saved_pos = s1;
Some(peg_f15(s2))
} else {
self.curr_pos = s1;
None
}
} else {
self.curr_pos = s1;
None
}; let s2 = self.parse_value();
if let Some(s2) = s2 {
self.saved_pos = s0;
Some(peg_f16(s1, s2))
} else {
self.curr_pos = s0;
None
}
}
fn parse_function_parameter_tail(&mut self) -> Option<Param> {
let s0 = self.curr_pos;
let mut s2 = self.parse__();
while s2.is_some() {
s2 = self.parse__();
}
self.rule_expects(&PEG_E7);
let s2 = if self.char_code_at(self.curr_pos) == Some(44) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s2.is_some() {
let Some(_level) = self.level(cost::PARAMETER_TAIL) else {
self.curr_pos = s0;
return None;
};
let mut s4 = self.parse__();
while s4.is_some() {
s4 = self.parse__();
}
let s4 = self.parse_function_parameter();
if let Some(s4) = s4 {
self.saved_pos = s0;
Some(peg_f17(s4))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_trailing_comma(&mut self) -> Option<()> {
let s0 = self.curr_pos;
let mut s2 = self.parse__();
while s2.is_some() {
s2 = self.parse__();
}
self.rule_expects(&PEG_E7);
let s2 = if self.char_code_at(self.curr_pos) == Some(44) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s2.is_some() {
Some(())
} else {
self.curr_pos = s0;
None
} }
fn parse_variable(&mut self) -> Option<Value> {
self.rule_expects(&PEG_E8);
self.silent_fails += 1;
let s0 = self.curr_pos;
let s1 = match self.char_code_at(self.curr_pos) {
Some(c @ (0x24 | 0x40)) => {
self.curr_pos += 1;
Some(c)
}
_ => None,
};
let result = if let Some(s1) = s1 {
let s2 = self.parse_identifier();
if let Some(s2) = s2 {
let mut s3 = Vec::new();
let mut s4 = self.parse_variable_tail();
while let Some(segment) = s4 {
s3.push(segment);
s4 = self.parse_variable_tail();
}
self.saved_pos = s0;
Some(peg_f18(s1, s2, s3))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
};
self.silent_fails -= 1;
result
}
fn parse_variable_tail(&mut self) -> Option<Value> {
let mut s0 = self.curr_pos;
let s1 = if self.char_code_at(self.curr_pos) == Some(46) {
self.curr_pos += 1;
Some(())
} else {
None
};
let mut result = if s1.is_some() {
let s2 = self.parse_identifier();
if let Some(s2) = s2 {
self.saved_pos = s0;
Some(Value::String(peg_f15(s2)))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
};
if result.is_none() {
s0 = self.curr_pos;
let s1 = if self.char_code_at(self.curr_pos) == Some(91) {
self.curr_pos += 1;
Some(())
} else {
None
};
result = if s1.is_some() {
let mut s2 = self.parse_value_number().map(Value::Number);
if s2.is_none() {
s2 = self.parse_value_string().map(Value::String);
}
if let Some(s2) = s2 {
let s3 = if self.char_code_at(self.curr_pos) == Some(93) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s3.is_some() {
self.saved_pos = s0;
Some(peg_f17(s2))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
};
}
result
}
fn level(&mut self, cost: u64) -> Option<crate::depth::Level> {
let level = crate::depth::enter(cost);
if level.is_none() {
self.too_deep = true;
}
level
}
fn parse_value(&mut self) -> Option<Value> {
if self.too_deep {
return None;
}
if self.depth == MAX_NESTING && !crate::depth::can_grow() {
self.too_deep = true;
return None;
}
self.depth += 1;
let s0 = crate::depth::grow!(self.parse_value_alternatives());
self.depth -= 1;
s0
}
#[inline(always)]
fn parse_value_alternatives(&mut self) -> Option<Value> {
let mut s0;
s0 = self.parse_value_null();
if s0.is_none() {
s0 = self.parse_value_boolean();
if s0.is_none() {
s0 = self.parse_value_string().map(Value::String);
if s0.is_none() {
s0 = self.parse_value_number().map(Value::Number);
if s0.is_none() {
s0 = self.parse_value_array();
if s0.is_none() {
s0 = self.parse_value_hash();
if s0.is_none() {
s0 = self.parse_function();
if s0.is_none() {
s0 = self.parse_variable();
}
}
}
}
}
}
}
s0
}
fn parse_value_null(&mut self) -> Option<Value> {
self.rule_expects(&PEG_E9);
self.silent_fails += 1;
let s0 = self.curr_pos;
let s1 = if self.substr_is(self.curr_pos, PEG_C9) {
self.curr_pos += 4;
Some(())
} else {
None
};
let result = if s1.is_some() {
self.saved_pos = s0;
Some(peg_f19())
} else {
None
};
self.silent_fails -= 1;
result
}
fn parse_value_boolean(&mut self) -> Option<Value> {
self.rule_expects(&PEG_E10);
self.silent_fails += 1;
let mut s0 = self.curr_pos;
let s1 = if self.substr_is(self.curr_pos, PEG_C10) {
self.curr_pos += 4;
Some(())
} else {
None
};
let mut result = if s1.is_some() {
self.saved_pos = s0;
Some(peg_f20())
} else {
None
};
if result.is_none() {
s0 = self.curr_pos;
let s1 = if self.substr_is(self.curr_pos, PEG_C11) {
self.curr_pos += 5;
Some(())
} else {
None
};
result = if s1.is_some() {
self.saved_pos = s0;
Some(peg_f21())
} else {
None
};
}
self.silent_fails -= 1;
result
}
fn parse_value_array(&mut self) -> Option<Value> {
let s0 = self.curr_pos;
self.rule_expects(&PEG_E11);
let s1 = if self.char_code_at(self.curr_pos) == Some(91) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s1.is_some() {
let Some(_level) = self.level(cost::ARRAY) else {
self.curr_pos = s0;
return None;
};
let mut s3 = self.parse__();
while s3.is_some() {
s3 = self.parse__();
}
let s3 = self.curr_pos;
let s4 = self.parse_value();
let s3: Option<Vec<Value>> = if let Some(s4) = s4 {
let mut s5 = Vec::new();
let mut s6 = self.parse_value_array_tail();
while let Some(item) = s6 {
s5.push(item);
s6 = self.parse_value_array_tail();
}
let _s6 = self.parse_trailing_comma();
self.saved_pos = s3;
Some(peg_f22(s4, s5))
} else {
self.curr_pos = s3;
None
}; let mut s5 = self.parse__();
while s5.is_some() {
s5 = self.parse__();
}
self.rule_expects(&PEG_E12);
let s5 = if self.char_code_at(self.curr_pos) == Some(93) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s5.is_some() {
self.saved_pos = s0;
Some(peg_f23(s3))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_value_array_tail(&mut self) -> Option<Value> {
let s0 = self.curr_pos;
let mut s2 = self.parse__();
while s2.is_some() {
s2 = self.parse__();
}
self.rule_expects(&PEG_E7);
let s2 = if self.char_code_at(self.curr_pos) == Some(44) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s2.is_some() {
let Some(_level) = self.level(cost::ARRAY_TAIL) else {
self.curr_pos = s0;
return None;
};
let mut s4 = self.parse__();
while s4.is_some() {
s4 = self.parse__();
}
let s4 = self.parse_value();
if let Some(s4) = s4 {
self.saved_pos = s0;
Some(peg_f17(s4))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_value_hash(&mut self) -> Option<Value> {
let s0 = self.curr_pos;
self.rule_expects(&PEG_E13);
let s1 = if self.char_code_at(self.curr_pos) == Some(123) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s1.is_some() {
let Some(_level) = self.level(cost::HASH) else {
self.curr_pos = s0;
return None;
};
let mut s3 = self.parse__();
while s3.is_some() {
s3 = self.parse__();
}
let s3 = self.curr_pos;
let s4 = self.parse_value_hash_item();
let s3: Option<Object> = if let Some(s4) = s4 {
let mut s5 = Vec::new();
let mut s6 = self.parse_value_hash_tail();
while let Some(item) = s6 {
s5.push(item);
s6 = self.parse_value_hash_tail();
}
let _s6 = self.parse_trailing_comma();
self.saved_pos = s3;
Some(peg_f24(s4, s5))
} else {
self.curr_pos = s3;
None
}; let mut s5 = self.parse__();
while s5.is_some() {
s5 = self.parse__();
}
self.rule_expects(&PEG_E14);
let s5 = if self.char_code_at(self.curr_pos) == Some(125) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s5.is_some() {
self.saved_pos = s0;
Some(peg_f25(s3))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_value_hash_tail(&mut self) -> Option<Object> {
let s0 = self.curr_pos;
let mut s2 = self.parse__();
while s2.is_some() {
s2 = self.parse__();
}
self.rule_expects(&PEG_E7);
let s2 = if self.char_code_at(self.curr_pos) == Some(44) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s2.is_some() {
let Some(_level) = self.level(cost::HASH_TAIL) else {
self.curr_pos = s0;
return None;
};
let mut s4 = self.parse__();
while s4.is_some() {
s4 = self.parse__();
}
let s4 = self.parse_value_hash_item();
if let Some(s4) = s4 {
self.saved_pos = s0;
Some(peg_f6(s4))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_value_hash_item(&mut self) -> Option<Object> {
let s0 = self.curr_pos;
let mut s1 = self.parse_identifier();
if s1.is_none() {
s1 = self.parse_value_string();
}
if let Some(s1) = s1 {
self.rule_expects(&PEG_E15);
let s2 = if self.char_code_at(self.curr_pos) == Some(58) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s2.is_some() {
let mut s4 = self.parse__();
while s4.is_some() {
s4 = self.parse__();
}
let s4 = self.parse_value();
if let Some(s4) = s4 {
self.saved_pos = s0;
Some(peg_f26(s1, s4))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_value_number(&mut self) -> Option<f64> {
self.rule_expects(&PEG_E16);
self.silent_fails += 1;
let s0 = self.curr_pos;
let _s1 = if self.char_code_at(self.curr_pos) == Some(45) {
self.curr_pos += 1;
Some(())
} else {
None
}; let s2 = if is_digit(self.char_code_at(self.curr_pos)) {
self.curr_pos += 1;
while is_digit(self.char_code_at(self.curr_pos)) {
self.curr_pos += 1;
}
Some(())
} else {
None
};
let result = if s2.is_some() {
let s3 = self.curr_pos;
let s4 = if self.char_code_at(self.curr_pos) == Some(46) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s4.is_some() {
let s5 = if is_digit(self.char_code_at(self.curr_pos)) {
self.curr_pos += 1;
while is_digit(self.char_code_at(self.curr_pos)) {
self.curr_pos += 1;
}
Some(())
} else {
None
};
if s5.is_none() {
self.curr_pos = s3;
}
} else {
self.curr_pos = s3;
} self.saved_pos = s0;
Some(peg_f27(&self.text()))
} else {
self.curr_pos = s0;
None
};
self.silent_fails -= 1;
result
}
fn parse_value_string(&mut self) -> Option<String> {
self.rule_expects(&PEG_E17);
self.silent_fails += 1;
let s0 = self.curr_pos;
let s1 = if self.char_code_at(self.curr_pos) == Some(34) {
self.curr_pos += 1;
Some(())
} else {
None
};
let result = if s1.is_some() {
let mut s2 = Vec::new();
let mut s3 = self.parse_value_string_chars();
while let Some(unit) = s3 {
s2.push(unit);
s3 = self.parse_value_string_chars();
}
let s3 = if self.char_code_at(self.curr_pos) == Some(34) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s3.is_some() {
self.saved_pos = s0;
Some(peg_f28(s2))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
};
self.silent_fails -= 1;
result
}
fn parse_value_string_chars(&mut self) -> Option<u16> {
let mut s0 = match self.char_code_at(self.curr_pos) {
Some(c) if !(c <= 0x1F || c == 0x22 || c == 0x5C) => {
self.curr_pos += 1;
Some(c)
}
_ => None,
};
if s0.is_none() {
s0 = self.parse_value_string_escapes();
}
s0
}
fn parse_value_string_escapes(&mut self) -> Option<u16> {
let s0 = self.curr_pos;
let s1 = if self.char_code_at(self.curr_pos) == Some(92) {
self.curr_pos += 1;
Some(())
} else {
None
};
if s1.is_some() {
let mut s2 = if self.char_code_at(self.curr_pos) == Some(34) {
self.curr_pos += 1;
Some(0x22) } else {
None
};
if s2.is_none() {
s2 = if self.char_code_at(self.curr_pos) == Some(92) {
self.curr_pos += 1;
Some(0x5C) } else {
None
};
if s2.is_none() {
let pos = self.curr_pos;
let s3 = if self.char_code_at(self.curr_pos) == Some(110) {
self.curr_pos += 1;
Some(())
} else {
None
};
s2 = if s3.is_some() {
self.saved_pos = pos;
Some(peg_f29())
} else {
None
};
if s2.is_none() {
let pos = self.curr_pos;
let s3 = if self.char_code_at(self.curr_pos) == Some(114) {
self.curr_pos += 1;
Some(())
} else {
None
};
s2 = if s3.is_some() {
self.saved_pos = pos;
Some(peg_f30())
} else {
None
};
if s2.is_none() {
let pos = self.curr_pos;
let s3 = if self.char_code_at(self.curr_pos) == Some(116) {
self.curr_pos += 1;
Some(())
} else {
None
};
s2 = if s3.is_some() {
self.saved_pos = pos;
Some(peg_f31())
} else {
None
};
}
}
}
}
if let Some(s2) = s2 {
self.saved_pos = s0;
Some(peg_f32(s2))
} else {
self.curr_pos = s0;
None
}
} else {
self.curr_pos = s0;
None
}
}
fn parse_identifier(&mut self) -> Option<String> {
self.rule_expects(&PEG_E18);
self.silent_fails += 1;
let s0 = self.curr_pos;
let s1 = if is_identifier_char(self.char_code_at(self.curr_pos)) {
self.curr_pos += 1;
while is_identifier_char(self.char_code_at(self.curr_pos)) {
self.curr_pos += 1;
}
Some(())
} else {
None
};
let result = if s1.is_some() {
Some(self.substring(s0, self.curr_pos))
} else {
None
};
self.silent_fails -= 1;
result
}
fn parse__(&mut self) -> Option<u16> {
self.rule_expects(&PEG_E19);
self.silent_fails += 1;
let s0 = match self.char_code_at(self.curr_pos) {
Some(c @ (0x20 | 0x0A | 0x09)) => {
self.curr_pos += 1;
Some(c)
}
_ => None,
};
self.silent_fails -= 1;
s0
}
}
fn is_digit(c: Option<u16>) -> bool {
matches!(c, Some(0x30..=0x39))
}
fn is_identifier_char(c: Option<u16>) -> bool {
matches!(
c,
Some(0x41..=0x5A | 0x61..=0x7A | 0x30..=0x39 | 0x5F | 0x2D)
)
}
struct Param {
name: Option<String>,
value: Value,
}
fn peg_f0(variable: Value) -> Parsed {
Parsed {
kind: ParsedType::Variable,
nesting: None,
meta: Meta {
tag: None,
attributes: None,
variable: Some(variable),
},
}
}
fn peg_f1(attributes: Vec<Attribute>) -> Parsed {
Parsed {
kind: ParsedType::Annotation,
nesting: None,
meta: Meta {
tag: None,
attributes: Some(Some(attributes)),
variable: None,
},
}
}
fn peg_f2(_tag: &str, value: Value) -> Value {
value
}
fn peg_f3(
tag: String,
primary: Option<Value>,
mut attributes: Option<Vec<Attribute>>,
close: Option<u16>,
) -> Parsed {
if let Some(primary) = primary.filter(Value::truthy) {
let list = attributes.get_or_insert_with(Vec::new);
list.insert(
0,
Attribute {
kind: AttributeType::Attribute,
name: "primary".to_string(),
value: primary,
},
);
}
let (kind, nesting) = if close.is_some() {
(ParsedType::Tag, 0)
} else {
(ParsedType::TagOpen, 1)
};
Parsed {
kind,
nesting: Some(nesting),
meta: Meta {
tag: Some(tag),
attributes: Some(attributes),
variable: None,
},
}
}
fn peg_f4(tag: String) -> Parsed {
Parsed {
kind: ParsedType::TagClose,
nesting: Some(-1),
meta: Meta {
tag: Some(tag),
attributes: None,
variable: None,
},
}
}
fn peg_f5(head: Attribute, tail: Vec<Attribute>) -> Vec<Attribute> {
let mut list = Vec::with_capacity(1 + tail.len());
list.push(head);
list.extend(tail);
list
}
fn peg_f6<T>(item: T) -> T {
item
}
fn peg_f7(ids: Attribute) -> Attribute {
ids
}
fn peg_f8(classes: Attribute) -> Attribute {
classes
}
fn peg_f9(attribute: Attribute) -> Attribute {
attribute
}
fn peg_f10(name: String) -> Attribute {
Attribute {
kind: AttributeType::Class,
name,
value: Value::Bool(true),
}
}
fn peg_f11(value: String) -> Attribute {
Attribute {
kind: AttributeType::Attribute,
name: "id".to_string(),
value: Value::String(value),
}
}
fn peg_f12(name: String, value: Value) -> Attribute {
Attribute {
kind: AttributeType::Attribute,
name,
value,
}
}
fn peg_f13(_name: &str, head: Option<Param>, tail: Vec<Param>) -> Vec<Param> {
match head {
Some(head) => {
let mut params = Vec::with_capacity(1 + tail.len());
params.push(head);
params.extend(tail);
params
}
None => Vec::new(),
}
}
fn peg_f14(name: String, params: Vec<Param>) -> Value {
let mut parameters = Object::new();
for (index, Param { name, value }) in params.into_iter().enumerate() {
parameters.set(name.unwrap_or_else(|| index.to_string()), value);
}
Value::Function(Function { name, parameters })
}
fn peg_f15(name: String) -> String {
name
}
fn peg_f16(name: Option<String>, value: Value) -> Param {
Param { name, value }
}
fn peg_f17<T>(value: T) -> T {
value
}
fn peg_f18(prefix: u16, head: String, tail: Vec<Value>) -> Value {
let mut path = Vec::with_capacity(1 + tail.len());
path.push(Value::String(head));
path.extend(tail);
if prefix == b'@' as u16 {
return Value::Array(path);
}
Value::Variable(Variable { path })
}
fn peg_f19() -> Value {
Value::Null
}
fn peg_f20() -> Value {
Value::Bool(true)
}
fn peg_f21() -> Value {
Value::Bool(false)
}
fn peg_f22(head: Value, tail: Vec<Value>) -> Vec<Value> {
let mut items = Vec::with_capacity(1 + tail.len());
items.push(head);
items.extend(tail);
items
}
fn peg_f23(value: Option<Vec<Value>>) -> Value {
Value::Array(value.unwrap_or_default())
}
fn peg_f24(mut head: Object, tail: Vec<Object>) -> Object {
for source in tail {
head.assign(source);
}
head
}
fn peg_f25(value: Option<Object>) -> Value {
Value::Object(value.unwrap_or_default())
}
fn peg_f26(key: String, value: Value) -> Object {
if key == "$$mdtype" {
Object::new()
} else {
Object::with_property(key, value)
}
}
fn peg_f27(text: &str) -> f64 {
parse_float(text)
}
fn parse_float(text: &str) -> f64 {
text.parse::<f64>()
.expect("ValueNumber matches only decimal literals")
}
fn peg_f28(value: Vec<u16>) -> String {
String::from_utf16_lossy(&value)
}
fn peg_f29() -> u16 {
0x0A
}
fn peg_f30() -> u16 {
0x0D
}
fn peg_f31() -> u16 {
0x09
}
fn peg_f32(sequence: u16) -> u16 {
sequence
}