<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Types used to report parsing errors.

#![deny(missing_docs)]

use cssparser::{BasicParseError, Token, SourceLocation};
use cssparser::ParseError as CssParseError;
use log;
use style_traits::ParseError;
use stylesheets::UrlExtraData;

/// Errors that can be encountered while parsing CSS.
pub enum ContextualParseError<'a> {
    /// A property declaration was not recognized.
    UnsupportedPropertyDeclaration(&'a str, ParseError<'a>),
    /// A font face descriptor was not recognized.
    UnsupportedFontFaceDescriptor(&'a str, ParseError<'a>),
    /// A font feature values descroptor was not recognized.
    UnsupportedFontFeatureValuesDescriptor(&'a str, ParseError<'a>),
    /// A keyframe rule was not valid.
    InvalidKeyframeRule(&'a str, ParseError<'a>),
    /// A font feature values rule was not valid.
    InvalidFontFeatureValuesRule(&'a str, ParseError<'a>),
    /// A keyframe property declaration was not recognized.
    UnsupportedKeyframePropertyDeclaration(&'a str, ParseError<'a>),
    /// A rule was invalid for some reason.
    InvalidRule(&'a str, ParseError<'a>),
    /// A rule was not recognized.
    UnsupportedRule(&'a str, ParseError<'a>),
    /// A viewport descriptor declaration was not recognized.
    UnsupportedViewportDescriptorDeclaration(&'a str, ParseError<'a>),
    /// A counter style descriptor declaration was not recognized.
    UnsupportedCounterStyleDescriptorDeclaration(&'a str, ParseError<'a>),
    /// A counter style rule had no symbols.
    InvalidCounterStyleWithoutSymbols(String),
    /// A counter style rule had less than two symbols.
    InvalidCounterStyleNotEnoughSymbols(String),
    /// A counter style rule did not have additive-symbols.
    InvalidCounterStyleWithoutAdditiveSymbols,
    /// A counter style rule had extends with symbols.
    InvalidCounterStyleExtendsWithSymbols,
    /// A counter style rule had extends with additive-symbols.
    InvalidCounterStyleExtendsWithAdditiveSymbols
}

impl<'a> ContextualParseError<'a> {
    /// Turn a parse error into a string representation.
    pub fn to_string(&self) -> String {
        fn token_to_str(t: &Token) -> String {
            match *t {
                Token::Ident(ref i) => format!("identifier {}", i),
                Token::AtKeyword(ref kw) => format!("keyword @{}", kw),
                Token::Hash(ref h) => format!("hash #{}", h),
                Token::IDHash(ref h) => format!("id selector #{}", h),
                Token::QuotedString(ref s) => format!("quoted string \"{}\"", s),
                Token::UnquotedUrl(ref u) => format!("url {}", u),
                Token::Delim(ref d) => format!("delimiter {}", d),
                Token::Number { int_value: Some(i), .. } => format!("number {}", i),
                Token::Number { value, .. } => format!("number {}", value),
                Token::Percentage { int_value: Some(i), .. } => format!("percentage {}", i),
                Token::Percentage { unit_value, .. } => format!("percentage {}", unit_value * 100.),
                Token::Dimension { value, ref unit, .. } => format!("dimension {}{}", value, unit),
                Token::WhiteSpace(_) => format!("whitespace"),
                Token::Comment(_) => format!("comment"),
                Token::Colon => format!("colon (:)"),
                Token::Semicolon => format!("semicolon (;)"),
                Token::Comma => format!("comma (,)"),
                Token::IncludeMatch => format!("include match (~=)"),
                Token::DashMatch => format!("dash match (|=)"),
                Token::PrefixMatch => format!("prefix match (^=)"),
                Token::SuffixMatch => format!("suffix match ($=)"),
                Token::SubstringMatch => format!("substring match (*=)"),
                Token::Column => format!("column (||)"),
                Token::CDO => format!("CDO (<!--)"),
                Token::CDC => format!("CDC (-->)"),
                Token::Function(ref f) => format!("function {}", f),
                Token::ParenthesisBlock => format!("parenthesis ("),
                Token::SquareBracketBlock => format!("square bracket ["),
                Token::CurlyBracketBlock => format!("curly bracket {{"),
                Token::BadUrl(ref _u) => format!("bad url parse error"),
                Token::BadString(ref _s) => format!("bad string parse error"),
                Token::CloseParenthesis => format!("unmatched close parenthesis"),
                Token::CloseSquareBracket => format!("unmatched close square bracket"),
                Token::CloseCurlyBracket => format!("unmatched close curly bracket"),
            }
        }

        fn parse_error_to_str(err: &ParseError) -> String {
            match *err {
                CssParseError::Basic(BasicParseError::UnexpectedToken(ref t)) =>
                    format!("found unexpected {}", token_to_str(t)),
                CssParseError::Basic(BasicParseError::EndOfInput) =>
                    format!("unexpected end of input"),
                CssParseError::Basic(BasicParseError::AtRuleInvalid(ref i)) =>
                    format!("@ rule invalid: {}", i),
                CssParseError::Basic(BasicParseError::AtRuleBodyInvalid) =>
                    format!("@ rule invalid"),
                CssParseError::Basic(BasicParseError::QualifiedRuleInvalid) =>
                    format!("qualified rule invalid"),
                CssParseError::Custom(ref err) =>
                    format!("{:?}", err)
            }
        }

        match *self {
            ContextualParseError::UnsupportedPropertyDeclaration(decl, ref err) =>
                format!("Unsupported property declaration: '{}', {}", decl,
                        parse_error_to_str(err)),
            ContextualParseError::UnsupportedFontFaceDescriptor(decl, ref err) =>
                format!("Unsupported @font-face descriptor declaration: '{}', {}", decl,
                        parse_error_to_str(err)),
            ContextualParseError::UnsupportedFontFeatureValuesDescriptor(decl, ref err) =>
            format!("Unsupported @font-feature-values descriptor declaration: '{}', {}", decl,
                    parse_error_to_str(err)),
            ContextualParseError::InvalidKeyframeRule(rule, ref err) =>
                format!("Invalid keyframe rule: '{}', {}", rule,
                        parse_error_to_str(err)),
            ContextualParseError::InvalidFontFeatureValuesRule(rule, ref err) =>
            format!("Invalid font feature value rule: '{}', {}", rule,
                    parse_error_to_str(err)),
            ContextualParseError::UnsupportedKeyframePropertyDeclaration(decl, ref err) =>
                format!("Unsupported keyframe property declaration: '{}', {}", decl,
                        parse_error_to_str(err)),
            ContextualParseError::InvalidRule(rule, ref err) =>
                format!("Invalid rule: '{}', {}", rule, parse_error_to_str(err)),
            ContextualParseError::UnsupportedRule(rule, ref err) =>
                format!("Unsupported rule: '{}', {}", rule, parse_error_to_str(err)),
            ContextualParseError::UnsupportedViewportDescriptorDeclaration(decl, ref err) =>
                format!("Unsupported @viewport descriptor declaration: '{}', {}", decl,
                        parse_error_to_str(err)),
            ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(decl, ref err) =>
                format!("Unsupported @counter-style descriptor declaration: '{}', {}", decl,
                        parse_error_to_str(err)),
            ContextualParseError::InvalidCounterStyleWithoutSymbols(ref system) =>
                format!("Invalid @counter-style rule: 'system: {}' without 'symbols'", system),
            ContextualParseError::InvalidCounterStyleNotEnoughSymbols(ref system) =>
                format!("Invalid @counter-style rule: 'system: {}' less than two 'symbols'", system),
            ContextualParseError::InvalidCounterStyleWithoutAdditiveSymbols =>
                "Invalid @counter-style rule: 'system: additive' without 'additive-symbols'".into(),
            ContextualParseError::InvalidCounterStyleExtendsWithSymbols =>
                "Invalid @counter-style rule: 'system: extends …' with 'symbols'".into(),
            ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols =>
                "Invalid @counter-style rule: 'system: extends …' with 'additive-symbols'".into(),
        }
    }
}<commit_msg>Reformat a couple match expressions<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Types used to report parsing errors.

#![deny(missing_docs)]

use cssparser::{BasicParseError, Token, SourceLocation};
use cssparser::ParseError as CssParseError;
use log;
use style_traits::ParseError;
use stylesheets::UrlExtraData;

/// Errors that can be encountered while parsing CSS.
pub enum ContextualParseError<'a> {
    /// A property declaration was not recognized.
    UnsupportedPropertyDeclaration(&'a str, ParseError<'a>),
    /// A font face descriptor was not recognized.
    UnsupportedFontFaceDescriptor(&'a str, ParseError<'a>),
    /// A font feature values descroptor was not recognized.
    UnsupportedFontFeatureValuesDescriptor(&'a str, ParseError<'a>),
    /// A keyframe rule was not valid.
    InvalidKeyframeRule(&'a str, ParseError<'a>),
    /// A font feature values rule was not valid.
    InvalidFontFeatureValuesRule(&'a str, ParseError<'a>),
    /// A keyframe property declaration was not recognized.
    UnsupportedKeyframePropertyDeclaration(&'a str, ParseError<'a>),
    /// A rule was invalid for some reason.
    InvalidRule(&'a str, ParseError<'a>),
    /// A rule was not recognized.
    UnsupportedRule(&'a str, ParseError<'a>),
    /// A viewport descriptor declaration was not recognized.
    UnsupportedViewportDescriptorDeclaration(&'a str, ParseError<'a>),
    /// A counter style descriptor declaration was not recognized.
    UnsupportedCounterStyleDescriptorDeclaration(&'a str, ParseError<'a>),
    /// A counter style rule had no symbols.
    InvalidCounterStyleWithoutSymbols(String),
    /// A counter style rule had less than two symbols.
    InvalidCounterStyleNotEnoughSymbols(String),
    /// A counter style rule did not have additive-symbols.
    InvalidCounterStyleWithoutAdditiveSymbols,
    /// A counter style rule had extends with symbols.
    InvalidCounterStyleExtendsWithSymbols,
    /// A counter style rule had extends with additive-symbols.
    InvalidCounterStyleExtendsWithAdditiveSymbols
}

impl<'a> ContextualParseError<'a> {
    /// Turn a parse error into a string representation.
    pub fn to_string(&self) -> String {
        fn token_to_str(t: &Token) -> String {
            match *t {
                Token::Ident(ref i) => format!("identifier {}", i),
                Token::AtKeyword(ref kw) => format!("keyword @{}", kw),
                Token::Hash(ref h) => format!("hash #{}", h),
                Token::IDHash(ref h) => format!("id selector #{}", h),
                Token::QuotedString(ref s) => format!("quoted string \"{}\"", s),
                Token::UnquotedUrl(ref u) => format!("url {}", u),
                Token::Delim(ref d) => format!("delimiter {}", d),
                Token::Number { int_value: Some(i), .. } => format!("number {}", i),
                Token::Number { value, .. } => format!("number {}", value),
                Token::Percentage { int_value: Some(i), .. } => format!("percentage {}", i),
                Token::Percentage { unit_value, .. } => format!("percentage {}", unit_value * 100.),
                Token::Dimension { value, ref unit, .. } => format!("dimension {}{}", value, unit),
                Token::WhiteSpace(_) => format!("whitespace"),
                Token::Comment(_) => format!("comment"),
                Token::Colon => format!("colon (:)"),
                Token::Semicolon => format!("semicolon (;)"),
                Token::Comma => format!("comma (,)"),
                Token::IncludeMatch => format!("include match (~=)"),
                Token::DashMatch => format!("dash match (|=)"),
                Token::PrefixMatch => format!("prefix match (^=)"),
                Token::SuffixMatch => format!("suffix match ($=)"),
                Token::SubstringMatch => format!("substring match (*=)"),
                Token::Column => format!("column (||)"),
                Token::CDO => format!("CDO (<!--)"),
                Token::CDC => format!("CDC (-->)"),
                Token::Function(ref f) => format!("function {}", f),
                Token::ParenthesisBlock => format!("parenthesis ("),
                Token::SquareBracketBlock => format!("square bracket ["),
                Token::CurlyBracketBlock => format!("curly bracket {{"),
                Token::BadUrl(ref _u) => format!("bad url parse error"),
                Token::BadString(ref _s) => format!("bad string parse error"),
                Token::CloseParenthesis => format!("unmatched close parenthesis"),
                Token::CloseSquareBracket => format!("unmatched close square bracket"),
                Token::CloseCurlyBracket => format!("unmatched close curly bracket"),
            }
        }

        fn parse_error_to_str(err: &ParseError) -> String {
            match *err {
                CssParseError::Basic(BasicParseError::UnexpectedToken(ref t)) => {
                    format!("found unexpected {}", token_to_str(t))
                }
                CssParseError::Basic(BasicParseError::EndOfInput) => {
                    format!("unexpected end of input")
                }
                CssParseError::Basic(BasicParseError::AtRuleInvalid(ref i)) => {
                    format!("@ rule invalid: {}", i)
                }
                CssParseError::Basic(BasicParseError::AtRuleBodyInvalid) => {
                    format!("@ rule invalid")
                }
                CssParseError::Basic(BasicParseError::QualifiedRuleInvalid) => {
                    format!("qualified rule invalid")
                }
                CssParseError::Custom(ref err) => {
                    format!("{:?}", err)
                }
            }
        }

        match *self {
            ContextualParseError::UnsupportedPropertyDeclaration(decl, ref err) => {
                format!("Unsupported property declaration: '{}', {}", decl,
                        parse_error_to_str(err))
            }
            ContextualParseError::UnsupportedFontFaceDescriptor(decl, ref err) => {
                format!("Unsupported @font-face descriptor declaration: '{}', {}", decl,
                        parse_error_to_str(err))
            }
            ContextualParseError::UnsupportedFontFeatureValuesDescriptor(decl, ref err) => {
                format!("Unsupported @font-feature-values descriptor declaration: '{}', {}", decl,
                        parse_error_to_str(err))
            }
            ContextualParseError::InvalidKeyframeRule(rule, ref err) => {
                format!("Invalid keyframe rule: '{}', {}", rule,
                        parse_error_to_str(err))
            }
            ContextualParseError::InvalidFontFeatureValuesRule(rule, ref err) => {
                format!("Invalid font feature value rule: '{}', {}", rule,
                        parse_error_to_str(err))
            }
            ContextualParseError::UnsupportedKeyframePropertyDeclaration(decl, ref err) => {
                format!("Unsupported keyframe property declaration: '{}', {}", decl,
                        parse_error_to_str(err))
            }
            ContextualParseError::InvalidRule(rule, ref err) => {
                format!("Invalid rule: '{}', {}", rule, parse_error_to_str(err))
            }
            ContextualParseError::UnsupportedRule(rule, ref err) => {
                format!("Unsupported rule: '{}', {}", rule, parse_error_to_str(err))
            }
            ContextualParseError::UnsupportedViewportDescriptorDeclaration(decl, ref err) => {
                format!("Unsupported @viewport descriptor declaration: '{}', {}", decl,
                        parse_error_to_str(err))
            }
            ContextualParseError::UnsupportedCounterStyleDescriptorDeclaration(decl, ref err) => {
                format!("Unsupported @counter-style descriptor declaration: '{}', {}", decl,
                        parse_error_to_str(err))
            }
            ContextualParseError::InvalidCounterStyleWithoutSymbols(ref system) => {
                format!("Invalid @counter-style rule: 'system: {}' without 'symbols'", system)
            }
            ContextualParseError::InvalidCounterStyleNotEnoughSymbols(ref system) => {
                format!("Invalid @counter-style rule: 'system: {}' less than two 'symbols'", system)
            }
            ContextualParseError::InvalidCounterStyleWithoutAdditiveSymbols => {
                "Invalid @counter-style rule: 'system: additive' without 'additive-symbols'".into()
            }
            ContextualParseError::InvalidCounterStyleExtendsWithSymbols => {
                "Invalid @counter-style rule: 'system: extends …' with 'symbols'".into()
            }
            ContextualParseError::InvalidCounterStyleExtendsWithAdditiveSymbols => {
                "Invalid @counter-style rule: 'system: extends …' with 'additive-symbols'".into()
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.13850477796514896, 'fraction_numerical': 0.0007869589657110736, 'mean_word_length': 2.466004285992597, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 147, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2213746', 'n_tokens_mistral': 4574, 'n_tokens_neox': 4588, 'n_words': 1050}