MCPcopy Create free account
hub / github.com/csskit/csskit / parse

Method parse

crates/css_ast/src/selector/nth.rs:23–124  ·  view source on GitHub ↗
(p: &mut Parser<'a, I>)

Source from the content-addressed store, hash-verified

21
22impl<'a> Parse<'a> for Nth {
23 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
24 where
25 I: Iterator<Item = Cursor> + Clone,
26 {
27 if p.peek::<CSSInt>() {
28 return Ok(Self::Integer(p.parse::<CSSInt>()?));
29 } else if p.peek::<T![Ident]>() {
30 let peek_cursor = p.peek_n(1);
31 let atom = p.to_atom::<CssAtomSet>(peek_cursor);
32 if atom == CssAtomSet::Odd {
33 let ident = p.parse::<T![Ident]>()?;
34 return Ok(Self::Odd(ident));
35 } else if atom == CssAtomSet::Even {
36 let ident = p.parse::<T![Ident]>()?;
37 return Ok(Self::Even(ident));
38 }
39 }
40
41 let mut c = p.next();
42
43 let a;
44 let mut b_sign = 0;
45 let mut cursors = [c, Cursor::EMPTY, Cursor::EMPTY, Cursor::EMPTY];
46
47 if c == '+' {
48 let skip = p.set_skip(KindSet::NONE);
49 c = p.next();
50 p.set_skip(skip);
51 debug_assert!(cursors[1] == Cursor::EMPTY);
52 cursors[1] = c;
53 }
54 if !matches!(c.token().kind(), Kind::Number | Kind::Dimension | Kind::Ident) {
55 Err(Diagnostic::new(c, Diagnostic::unexpected))?
56 }
57 if c.token().is_float() {
58 Err(Diagnostic::new(c, Diagnostic::expected_int))?
59 }
60
61 if p.equals_atom(c, &CssAtomSet::_NDash) {
62 b_sign = -1;
63 a = if c.token().is_int() { c.token().value() as i32 } else { 1 };
64 } else {
65 let source_cursor = p.to_source_cursor(c);
66 let anb = source_cursor.parse(p.bump());
67 let mut chars = anb.chars();
68 let mut char = chars.next();
69 a = if c.token().is_int() {
70 c.token().value() as i32
71 } else if char == Some('-') {
72 char = chars.next();
73 -1
74 } else {
75 1
76 };
77 if !matches!(char, Some('n') | Some('N')) {
78 Err(Diagnostic::new(c, Diagnostic::unexpected))?
79 }
80 if let Ok(b) = chars.as_str().parse::<i32>() {

Callers

nothing calls this directly

Calls 15

ErrClass · 0.85
peek_nMethod · 0.80
set_skipMethod · 0.80
nextMethod · 0.45
is_floatMethod · 0.45
tokenMethod · 0.45
equals_atomMethod · 0.45
is_intMethod · 0.45
valueMethod · 0.45
to_source_cursorMethod · 0.45
bumpMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected