lewp_css/
custom_parse_error.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    crate::domain::at_rules::counter_style::System,
6    cssparser::{
7        BasicParseErrorKind,
8        CowRcStr,
9        ParseError,
10        ParseErrorKind,
11        SourceLocation,
12        Token,
13    },
14    selectors::parser::{SelectorParseError, SelectorParseErrorKind},
15};
16
17/// Represents all the things that can go wrong when parsing.
18#[derive(Debug)]
19pub enum CustomParseError<'i> {
20    // @-rule
21    UnsupportedAtRule(CowRcStr<'i>),
22    InvalidParseState,
23
24    // @charset
25    UnexpectedCharsetAtRule,
26
27    // @counter-style
28    UnsupportedCounterStyleProperty(CowRcStr<'i>),
29    InvalidCounterStyleWithoutSymbols(System),
30    InvalidCounterStyleNotEnoughSymbols(System),
31    InvalidCounterStyleWithoutAdditiveSymbols,
32    InvalidCounterStyleExtendsWithSymbols,
33    InvalidCounterStyleExtendsWithAdditiveSymbols,
34    CounterStyleSystemIsNotKnown(CowRcStr<'i>),
35    CounterStyleSymbolsCanNotBeEmpty,
36    CounterStyleRangesCanNotHaveStartGreaterThanEnd(i32, i32),
37    CounterStylePadMinLengthCanNotBeNegative(i32),
38    CounterStyleAdditiveTupleWeightCanNotBeNegative(i32),
39    CounterStyleAdditiveSymbolsCanNotHaveASecondWeightEqualToOrGreaterThanTheFirst,
40    DecimalOrDiscIsNotAllowedInACounterStyleIdentInACounterStyleAtRule,
41    NoneIsNotAllowedInACounterStyleIdent,
42
43    // @document
44    DocumentAtRuleUrlMatchingFunctionWasInvalid,
45    BadUrlInDeclarationValueBlock(CowRcStr<'i>),
46    BadStringInDeclarationValueBlock(CowRcStr<'i>),
47    UnbalancedCloseParenthesisInDeclarationValueBlock,
48    UnbalancedCloseSquareBracketInDeclarationValueBlock,
49    UnbalancedCloseCurlyBracketInDeclarationValueBlock,
50
51    // @font-face
52    UnsupportedFontFaceProperty(CowRcStr<'i>),
53
54    // @font-feature-values
55    InvalidFontLanguageOverrideIdentifier(CowRcStr<'i>),
56    InvalidFontLanguageOverrideOpenTypeLanguageTag(CowRcStr<'i>),
57    FontFeatureSettingOpenTypeFeatureTagMustBeFourCharacters(CowRcStr<'i>),
58    FontFeatureSettingOpenTypeFeatureTagMustBePrintableAscii(CowRcStr<'i>),
59    FontFeatureSettingIfNotAnIntegerMustBeOnOrOff(CowRcStr<'i>),
60    FontFeatureSettingIntegerMustBePositive(i32),
61    FontFaceAtRuleFontWeightWasNotAValidIdentifierOrInteger,
62    FontFaceAtRuleFontFamilyCanNotBeGeneric,
63
64    // @import
65    AtRuleImportMustBeBeforeAnyRuleExceptAtRuleCharset,
66
67    // @keyframes
68    KeyframePercentageWasNotBetweenZeroAndOneInclusive(f32),
69    ImportantIsNotAllowedInKeyframePropertyDeclarationValues,
70    UnexpectedTokenWhenParsingZoom(Token<'i>),
71
72    // @media
73    InvalidMediaType(CowRcStr<'i>),
74    DeprecatedMediaType(CowRcStr<'i>),
75    UnrecognisedMediaType(CowRcStr<'i>),
76    DeprecatedMediaQueryExpression(CowRcStr<'i>),
77    UnsupportedMediaQueryExpression(CowRcStr<'i>),
78    RatioNumeratorCanNotBeNegativeOrZero(i32),
79    RatioDivisorCanNotBeNegativeOrZero(i32),
80    MediaGridMustBeEitherZeroOrOne(i32),
81    MediaTransform3DMustBeEitherZeroOrOne(i32),
82    MediaTypeIsOnlyOptionalIfQualifiedIsNotSpecified,
83
84    // @namespace
85    AtRuleNamespaceMustBeBeforeAnyRuleExceptAtRuleCharsetAndAtRuleImport,
86    UnexpectedTokenForAtNamespaceRuleNamespaceValue(Token<'i>),
87
88    // @page
89    InvalidPageSelectorPseudoClass(CowRcStr<'i>),
90    FontRelativeLengthsAreNotAllowedInAPageAtRule,
91    ViewportLengthsAreNotAllowedInAPageAtRule,
92
93    // @supports
94    InvalidSupportsCondition(CowRcStr<'i>),
95
96    // @viewport
97    UnexpectedViewportProperty(CowRcStr<'i>),
98
99    // selectors
100    SpecificSelectorParseError(Box<SelectorParseError<'i>>),
101    ThereAreNoSelectors,
102    SelectorIsInvalidInContext(String),
103    UnsupportedPseudoClassOrElement(String),
104    NonTreeStructuralPseudoClassScopeIsObsoleteAsOfFirefox55,
105
106    // custom ident
107    UnexpectedCustomIdent(CowRcStr<'i>),
108    CustomIdentWasExcluded(CowRcStr<'i>),
109
110    // numbers & units
111    CouldNotParseCssSignedNumber(
112        crate::domain::numbers::CssNumberConversionError,
113        f32,
114    ),
115    CouldNotParseCssUnsignedNumber(
116        crate::domain::numbers::CssNumberConversionError,
117        f32,
118    ),
119    CouldNotParseDimensionLessNumber(f32),
120    CouldNotParseDimension(f32, CowRcStr<'i>),
121    UnsignedIntegersCanNotBeNegative(i32),
122    UnsignedIntegersCanNotBeFloats(f32),
123
124    // expressions (calc(), var(), attr())
125    UnknownFunctionInValueExpression(CowRcStr<'i>),
126    CssVariablesInVarExpressionsMustStartWithTwoDashes(CowRcStr<'i>),
127
128    // required for From<SelectorParseErrorKind>
129    SelectorParseErrorKind(SelectorParseErrorKind<'i>),
130}
131
132impl<'i> CustomParseError<'i> {
133    #[inline(always)]
134    pub(crate) fn unexpectedToken<T>(
135        unexpectedToken: &Token<'i>,
136    ) -> Result<T, ParseError<'i, CustomParseError<'i>>> {
137        Err(ParseError {
138            kind: ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken(
139                unexpectedToken.clone(),
140            )),
141            location: SourceLocation { line: 0, column: 0 },
142        })
143    }
144
145    #[inline(always)]
146    pub(crate) fn dimensionless<T>(
147        value: f32,
148    ) -> Result<T, ParseError<'i, CustomParseError<'i>>> {
149        Err(ParseError {
150            kind: ParseErrorKind::Custom(
151                CustomParseError::CouldNotParseDimensionLessNumber(value),
152            ),
153            location: SourceLocation {
154                line: value as u32,
155                column: 0,
156            },
157        })
158    }
159}
160
161impl<'i> From<CustomParseError<'i>> for ParseError<'i, CustomParseError<'i>> {
162    fn from(error: CustomParseError) -> ParseError<CustomParseError> {
163        ParseError {
164            kind: ParseErrorKind::Custom(error),
165            location: SourceLocation { line: 0, column: 0 },
166        }
167    }
168}
169
170impl<'i> From<SelectorParseErrorKind<'i>> for CustomParseError<'i> {
171    fn from(error: SelectorParseErrorKind) -> CustomParseError {
172        CustomParseError::SelectorParseErrorKind(error)
173    }
174}