This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
ac606e2
commit a3a13d7
Showing
25 changed files
with
661 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,120 @@ | ||
//! Extremely fast, lossless, and error tolerant CSS Parser. | ||
use crate::parser::CssParser; | ||
|
||
use crate::syntax::parse_root; | ||
pub use parser::CssParserOptions; | ||
use rome_css_factory::CssSyntaxFactory; | ||
use rome_css_syntax::{CssLanguage, CssRoot, CssSyntaxNode}; | ||
pub use rome_parser::prelude::*; | ||
use rome_parser::tree_sink::LosslessTreeSink; | ||
use rome_rowan::{AstNode, NodeCache}; | ||
|
||
mod lexer; | ||
mod parser; | ||
mod prelude; | ||
mod syntax; | ||
mod token_source; | ||
|
||
pub(crate) type CssLosslessTreeSink<'source> = | ||
LosslessTreeSink<'source, CssLanguage, CssSyntaxFactory>; | ||
|
||
pub fn parse_css(source: &str, options: CssParserOptions) -> CssParse { | ||
let mut cache = NodeCache::default(); | ||
parse_css_with_cache(source, &mut cache, options) | ||
} | ||
|
||
/// Parses the provided string as CSS program using the provided node cache. | ||
pub fn parse_css_with_cache( | ||
source: &str, | ||
cache: &mut NodeCache, | ||
config: CssParserOptions, | ||
) -> CssParse { | ||
tracing::debug_span!("parse").in_scope(move || { | ||
let mut parser = CssParser::new(source, config); | ||
|
||
parse_root(&mut parser); | ||
|
||
let (events, diagnostics, trivia) = parser.finish(); | ||
|
||
let mut tree_sink = CssLosslessTreeSink::with_cache(source, &trivia, cache); | ||
rome_parser::event::process(&mut tree_sink, events, diagnostics); | ||
let (green, diagnostics) = tree_sink.finish(); | ||
|
||
CssParse::new(green, diagnostics) | ||
}) | ||
} | ||
|
||
/// A utility struct for managing the result of a parser job | ||
#[derive(Debug)] | ||
pub struct CssParse { | ||
root: CssSyntaxNode, | ||
diagnostics: Vec<ParseDiagnostic>, | ||
} | ||
|
||
impl CssParse { | ||
pub fn new(root: CssSyntaxNode, diagnostics: Vec<ParseDiagnostic>) -> CssParse { | ||
CssParse { root, diagnostics } | ||
} | ||
|
||
/// The syntax node represented by this Parse result | ||
/// | ||
/// ``` | ||
/// # use rome_css_parser::parse_css; | ||
/// # use rome_css_syntax::CssSyntaxKind; | ||
/// # use rome_rowan::{AstNode, AstNodeList, SyntaxError}; | ||
/// | ||
/// # fn main() -> Result<(), SyntaxError> { | ||
/// use rome_css_syntax::CssSyntaxKind; | ||
/// use rome_css_parser::CssParserOptions; | ||
/// let parse = parse_css(r#""#, CssParserOptions::default()); | ||
/// | ||
/// let root_value = parse.tree().rules(); | ||
/// | ||
/// assert_eq!(root_value.syntax().kind(), CssSyntaxKind::CSS_RULE_LIST); | ||
/// | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn syntax(&self) -> CssSyntaxNode { | ||
self.root.clone() | ||
} | ||
|
||
/// Get the diagnostics which occurred when parsing | ||
pub fn diagnostics(&self) -> &[ParseDiagnostic] { | ||
&self.diagnostics | ||
} | ||
|
||
/// Get the diagnostics which occurred when parsing | ||
pub fn into_diagnostics(self) -> Vec<ParseDiagnostic> { | ||
self.diagnostics | ||
} | ||
|
||
/// Returns [true] if the parser encountered some errors during the parsing. | ||
pub fn has_errors(&self) -> bool { | ||
self.diagnostics | ||
.iter() | ||
.any(|diagnostic| diagnostic.is_error()) | ||
} | ||
|
||
/// Convert this parse result into a typed AST node. | ||
/// | ||
/// # Panics | ||
/// Panics if the node represented by this parse result mismatches. | ||
pub fn tree(&self) -> CssRoot { | ||
CssRoot::unwrap_cast(self.syntax()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{parse_css, CssParserOptions}; | ||
|
||
#[test] | ||
fn parser_smoke_test() { | ||
let src = r#" | ||
"#; | ||
|
||
let _css = parse_css(src, CssParserOptions::default()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::token_source::CssTokenSource; | ||
use rome_css_syntax::CssSyntaxKind; | ||
use rome_parser::diagnostic::merge_diagnostics; | ||
use rome_parser::event::Event; | ||
use rome_parser::prelude::*; | ||
use rome_parser::token_source::Trivia; | ||
use rome_parser::ParserContext; | ||
|
||
pub(crate) struct CssParser<'source> { | ||
context: ParserContext<CssSyntaxKind>, | ||
source: CssTokenSource<'source>, | ||
} | ||
|
||
#[derive(Default, Debug, Clone, Copy)] | ||
pub struct CssParserOptions { | ||
pub allow_single_line_comments: bool, | ||
} | ||
|
||
impl CssParserOptions { | ||
pub fn with_allow_single_line_comments(mut self) -> Self { | ||
self.allow_single_line_comments = true; | ||
self | ||
} | ||
} | ||
|
||
impl<'source> CssParser<'source> { | ||
pub fn new(source: &'source str, config: CssParserOptions) -> Self { | ||
Self { | ||
context: ParserContext::default(), | ||
source: CssTokenSource::from_str(source, config), | ||
} | ||
} | ||
|
||
pub fn finish(self) -> (Vec<Event<CssSyntaxKind>>, Vec<ParseDiagnostic>, Vec<Trivia>) { | ||
let (trivia, lexer_diagnostics) = self.source.finish(); | ||
let (events, parse_diagnostics) = self.context.finish(); | ||
|
||
let diagnostics = merge_diagnostics(lexer_diagnostics, parse_diagnostics); | ||
|
||
(events, diagnostics, trivia) | ||
} | ||
} | ||
|
||
impl<'source> Parser for CssParser<'source> { | ||
type Kind = CssSyntaxKind; | ||
type Source = CssTokenSource<'source>; | ||
|
||
fn context(&self) -> &ParserContext<Self::Kind> { | ||
&self.context | ||
} | ||
|
||
fn context_mut(&mut self) -> &mut ParserContext<Self::Kind> { | ||
&mut self.context | ||
} | ||
|
||
fn source(&self) -> &Self::Source { | ||
&self.source | ||
} | ||
|
||
fn source_mut(&mut self) -> &mut Self::Source { | ||
&mut self.source | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use crate::parser::CssParser; | ||
use rome_css_syntax::CssSyntaxKind::*; | ||
use rome_parser::Parser; | ||
|
||
pub(crate) fn parse_root(p: &mut CssParser) { | ||
let m = p.start(); | ||
|
||
let rules = p.start(); | ||
|
||
rules.complete(p, CSS_RULE_LIST); | ||
|
||
m.complete(p, CSS_ROOT); | ||
} |
Oops, something went wrong.