Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(css/parser): union inputs #6385

Merged
merged 4 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/swc_css_parser/src/lexer/mod.rs
Expand Up @@ -140,7 +140,7 @@ where
{
type State = LexerState;

fn start_pos(&mut self) -> swc_common::BytePos {
fn start_pos(&mut self) -> BytePos {
self.input.cur_pos()
}

Expand Down
38 changes: 10 additions & 28 deletions crates/swc_css_parser/src/lib.rs
Expand Up @@ -5,13 +5,13 @@
#![allow(clippy::nonminimal_bool)]
#![allow(clippy::wrong_self_convention)]

use swc_common::{input::StringInput, BytePos, SourceFile};
use swc_common::{input::StringInput, SourceFile};

use crate::{
error::Error,
lexer::Lexer,
parser::{
input::{Tokens, TokensInput},
input::{Input, InputType},
PResult, Parser, ParserConfig,
},
};
Expand All @@ -37,28 +37,6 @@ where
}
}

/// Parse a given string as `T`.
///
/// If there are syntax errors but if it was recoverable, it will be appended
/// to `errors`.
pub fn parse_str<'a, T>(
src: &'a str,
start_pos: BytePos,
end_pos: BytePos,
config: ParserConfig,
errors: &mut Vec<Error>,
) -> PResult<T>
where
Parser<Lexer<StringInput<'a>>>: Parse<T>,
{
let lexer = Lexer::new(StringInput::new(src, start_pos, end_pos), config);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need it:

  • ecma and html doesn't support it in favor FileName::Anon for SourceFile
  • You can use directly Lexer::new(...) if you want to override spans

let mut parser = Parser::new(lexer, config);

let res = parser.parse();
errors.extend(parser.take_errors());
res
}

/// Parse a given file as `T`.
///
/// If there are syntax errors but if it was recoverable, it will be appended
Expand All @@ -75,26 +53,30 @@ where
let mut parser = Parser::new(lexer, config);

let res = parser.parse();

errors.extend(parser.take_errors());

res
}

/// Parse a given file as `T`.
///
/// If there are syntax errors but if it was recoverable, it will be appended
/// to `errors`.
pub fn parse_tokens<'a, T>(
tokens: &'a Tokens,
pub fn parse_input<'a, T>(
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
input: InputType<'a>,
config: ParserConfig,
errors: &mut Vec<Error>,
) -> PResult<T>
where
Parser<TokensInput<'a>>: Parse<T>,
Parser<Input<'a>>: Parse<T>,
{
let lexer = TokensInput::new(tokens);
let lexer = Input::new(input);
let mut parser = Parser::new(lexer, config);

let res = parser.parse();

errors.extend(parser.take_errors());

res
}