chore(xtask): choose language when generating grammar
MODIFY
Cargo.lock
Cargo.lock
@@ -1692,9 +1692,9 @@ dependencies = [
[[package]]
name = "termcolor"
-version = "1.1.2"
+version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
+checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]

MODIFY
xtask/codegen/src/ast.rs
xtask/codegen/src/ast.rs
//! This is derived from rust-analyzer/xtask/codegen
use std::collections::{HashMap, HashSet, VecDeque};
+use std::str::FromStr;
use std::vec;
use super::{
@@ -12,6 +13,8 @@ use crate::css_kinds_src::CSS_KINDS_SRC;
use crate::generate_syntax_factory::generate_syntax_factory;
use crate::json_kinds_src::JSON_KINDS_SRC;
use crate::kinds_src::{AstListSeparatorConfiguration, AstListSrc, TokenKind};
+use crate::termcolorful::{println_string_with_fg_color, Color};
+use crate::ALL_LANGUAGE_KIND;
use crate::{
generate_macros::generate_macros,
generate_nodes::generate_nodes,
@@ -25,18 +28,37 @@ use xtask::{project_root, Result};
// these node won't generate any code
pub const SYNTAX_ELEMENT_TYPE: &str = "SyntaxElement";
-pub fn generate_ast(mode: Mode) -> Result<()> {
- let mut ast = load_js_ast();
- ast.sort();
- generate_syntax(ast, &mode, LanguageKind::Js)?;
-
- let mut ast = load_css_ast();
- ast.sort();
- generate_syntax(ast, &mode, LanguageKind::Css)?;
-
- let mut ast = load_json_ast();
+pub fn generate_ast(mode: Mode, language_kind_list: Vec<String>) -> Result<()> {
+ let codegen_language_kinds = if language_kind_list.is_empty() {
+ ALL_LANGUAGE_KIND.clone().to_vec()
+ } else {
+ language_kind_list
+ .iter()
+ .filter_map(|kind| match LanguageKind::from_str(kind) {
+ Ok(kind) => Some(kind),
+ Err(err) => {
+ println_string_with_fg_color(err, Color::Red);
+ None
+ }
+ })
+ .collect::<Vec<_>>()
+ };
+ for kind in codegen_language_kinds {
+ println_string_with_fg_color(
+ format!(
+ "-------------------Generating AST for {}-------------------",
+ kind
+ ),
+ Color::Green,
+ );
+ let mut ast = match kind {
+ LanguageKind::Js => load_js_ast(),
+ LanguageKind::Css => load_css_ast(),
+ LanguageKind::Json => load_json_ast(),
+ };
ast.sort();
- generate_syntax(ast, &mode, LanguageKind::Json)?;
+ generate_syntax(ast, &mode, kind)?;
+ }
Ok(())
}

MODIFY
xtask/codegen/src/lib.rs
xtask/codegen/src/lib.rs
@@ -11,11 +11,12 @@ mod generate_syntax_kinds;
mod json_kinds_src;
mod kinds_src;
mod parser_tests;
+mod termcolorful;
mod unicode;
-
use proc_macro2::TokenStream;
use quote::quote;
use std::path::Path;
+use std::str::FromStr;
use xtask::{glue::fs2, Mode, Result};
@@ -51,6 +52,35 @@ pub enum LanguageKind {
Json,
}
+impl std::fmt::Display for LanguageKind {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ match self {
+ LanguageKind::Js => write!(f, "js"),
+ LanguageKind::Css => write!(f, "css"),
+ LanguageKind::Json => write!(f, "json"),
+ }
+ }
+}
+
+pub const ALL_LANGUAGE_KIND: [LanguageKind; 3] =
+ [LanguageKind::Js, LanguageKind::Css, LanguageKind::Json];
+
+impl FromStr for LanguageKind {
+ type Err = String;
+
+ fn from_str(kind: &str) -> Result<Self, Self::Err> {
+ match kind {
+ "js" => Ok(LanguageKind::Js),
+ "css" => Ok(LanguageKind::Css),
+ "json" => Ok(LanguageKind::Json),
+ _ => Err(format!(
+ "Language {} not supported, please use: `js`, `css` or `json`",
+ kind
+ )),
+ }
+ }
+}
+
impl LanguageKind {
pub(crate) fn syntax_kind(&self) -> TokenStream {
match self {

MODIFY
xtask/codegen/src/main.rs
xtask/codegen/src/main.rs
@@ -8,10 +8,14 @@ fn main() -> Result<()> {
let mut args = Arguments::from_env();
let command = args.subcommand()?.unwrap_or_default();
-
match command.as_str() {
"grammar" => {
- generate_ast(Mode::Overwrite)?;
+ let arg_list = args.finish();
+ let language_list = arg_list
+ .into_iter()
+ .filter_map(|arg| arg.to_str().map(|item| item.to_string()))
+ .collect::<Vec<_>>();
+ generate_ast(Mode::Overwrite, language_list)?;
Ok(())
}
"formatter" => {

ADD
xtask/codegen/src/termcolorful.rs
+#[derive(Copy, Clone, Debug)]
+#[allow(dead_code)]
+pub(crate) enum Color {
+ Red,
+ Green,
+ Black,
+ Yellow,
+ Blue,
+ Purple,
+ Cyan,
+ White,
+}
+impl std::fmt::Display for Color {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match *self {
+ Color::Black => write!(f, "30"),
+ Color::Red => write!(f, "31"),
+ Color::Green => write!(f, "32"),
+ Color::Yellow => write!(f, "33"),
+ Color::Blue => write!(f, "34"),
+ Color::Purple => write!(f, "35"),
+ Color::Cyan => write!(f, "36"),
+ Color::White => write!(f, "37"),
+ }
+ }
+}
+pub(crate) fn println_string_with_fg_color(content: String, color: Color) {
+ println!("\x1b[0;{}m{}\x1b[0m", color, content);
+}

chore(xtask): choose language when generating grammar (#2473)