Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Specy committed Sep 15, 2024
1 parent 66b59be commit c0c13a3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
30 changes: 15 additions & 15 deletions bindings/typescript/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import type {Action, AtomicPattern, Item, Rule, Token, Tree} from "./types";

export function stringifyToken(token: Token) {
export function stringifyToken(token: Token, noApostrophes = false) {
if (token.type === 'Eof') return "$"
if (token.type === "Regex") return `%${token.value}`
if (token.type === "Constant") return `'${token.value}'`
if (token.type === "Constant") return noApostrophes ? token.value : `'${token.value}'`
return ""
}

export function stringifyAtom(atom: AtomicPattern) {
export function stringifyAtom(atom: AtomicPattern, noApostrophes = false) {
if (atom.type === 'Symbol') return atom.value
if (atom.type === 'Token') return stringifyToken(atom.value)
if (atom.type === 'Token') return stringifyToken(atom.value, noApostrophes)
return ""
}

export function stringifyItem(item: Item) {
const children = item.rule.pattern.map(stringifyAtom)
export function stringifyItem(item: Item, noApostrophes = false) {
const children = item.rule.pattern.map((a) => stringifyAtom(a, noApostrophes))
//inserts the dot
children.splice(item.dot, 0, '.')
return `${item.rule.symbol} -> ${children.join(' ')}`
}

export function stringifyRule(rule: Rule) {
const children = rule.pattern.map(stringifyAtom)
export function stringifyRule(rule: Rule, noApostrophes = false) {
const children = rule.pattern.map(a => stringifyAtom(a, noApostrophes))
return `${rule.symbol} -> ${children.join(' ')}`
}

export function stringifyLookahead(item: Token[]) {
const children = item.map(stringifyToken)
export function stringifyLookahead(item: Token[], noApostrophes = false) {
const children = item.map(t => stringifyToken(t, noApostrophes))
return children.join(" ")
}

Expand All @@ -39,20 +39,20 @@ export function stringifyAction(action: Action) {
}


export function stringifyActionVerbose(action: Action, rules: Rule[]) {
export function stringifyActionVerbose(action: Action, rules: Rule[], noApostrophes: boolean = false) {
if (action.type === "Shift") {
return `Shift ${action.value.next_state}`
} else if (action.type === "Accept") {
return `Accept ${action.value.rule_index + 1} (${stringifyRule(rules[action.value.rule_index])})`
return `Accept ${action.value.rule_index + 1} (${stringifyRule(rules[action.value.rule_index], noApostrophes)})`
} else if (action.type === "Reduce") {
return `Reduce ${action.value.rule_index + 1} (${stringifyRule(rules[action.value.rule_index])})`
return `Reduce ${action.value.rule_index + 1} (${stringifyRule(rules[action.value.rule_index], noApostrophes)})`
}
return ""
}

export function stringifyTreeStack(tree: Tree[]): string[] {
export function stringifyTreeStack(tree: Tree[], noApostrophes = false): string[] {
return tree.map(i => {
if (i.type === "Terminal") return stringifyToken(i.value.token)
if (i.type === "Terminal") return stringifyToken(i.value.token, noApostrophes)
if (i.type === "NonTerminal") return i.value.symbol
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub enum ParserError {
format_smolstr!("{}", token).green(),
)]
Conflict {
#[cfg_attr(feature = "serde", serde(skip))] //TODO improve this
#[cfg_attr(feature = "serde", serde(skip))] // TODO improve this
parser: Box<Parser>,
state: usize,
token: Token,
Expand Down
15 changes: 7 additions & 8 deletions src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<T: Into<SmolStr>> From<T> for RegexToken {

/// Token (e.g., `'+'`, `%f`, `$`) in a grammar.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature="serde", serde(tag = "type", content = "value"))]
#[cfg_attr(feature = "serde", serde(tag = "type", content = "value"))]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Token {
/// Constant token.
Expand Down Expand Up @@ -123,7 +123,7 @@ impl From<RegexToken> for Token {

/// Elements (e.g., `E`, `'+'`, `%f`) of the pattern of a rule.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature="serde", serde(tag = "type", content = "value"))]
#[cfg_attr(feature = "serde", serde(tag = "type", content = "value"))]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum AtomicPattern {
/// Symbol to match.
Expand Down Expand Up @@ -219,7 +219,6 @@ pub struct Grammar {
}



impl Grammar {
/// Creates a grammar from a grammar string.
pub fn parse(grammar_string: &str) -> Result<Grammar, GrammarError> {
Expand All @@ -229,7 +228,7 @@ impl Grammar {

#[cfg(feature = "wasm")]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl Grammar{
impl Grammar {
pub fn parse_wasm(grammar_string: &str) -> Result<Grammar, JsValue> {
match Grammar::parse(grammar_string) {
Ok(grammar) => Ok(grammar),
Expand Down Expand Up @@ -272,19 +271,19 @@ impl Grammar {
pub fn symbols_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.symbols)?)
}
pub fn start_symbol_wasm(&self) -> Result<JsValue, JsValue> {
pub fn start_symbol_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.start_symbol)?)
}
pub fn rules_wasm(&self) -> Result<JsValue, JsValue> {
pub fn rules_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.rules)?)
}
pub fn to_string_wasm(&self) -> String {
self.to_string()
}
pub fn constant_tokens_wasm(&self) -> Result<JsValue, JsValue> {
pub fn constant_tokens_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.constant_tokens)?)
}

pub fn clone_wasm(&self) -> Grammar {
self.clone()
}
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ pub use {
};

mod prelude {
#[cfg(feature = "wasm")]
pub use wasm_bindgen::prelude::*;
#[cfg(feature="serde_support")]
#[cfg(feature = "serde_support")]
pub use serde::{
Deserialize,
Serialize,
};

#[cfg(target_family = "wasm")]
pub use utils::MockColored;
#[cfg(feature = "wasm")]
pub use wasm_bindgen::prelude::*;

#[cfg(not(target_family = "wasm"))]
pub use colored::*;

#[cfg(target_family = "wasm")]
pub use utils::MockColored;

pub use {
super::*,
indexmap::{
Expand Down
19 changes: 9 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Parser {
}
#[cfg(feature = "wasm")]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl Parser{
impl Parser {
pub fn new_wasm(grammar: Grammar) -> Result<Parser, JsValue> {
match Parser::lr(grammar) {
Ok(parser) => Ok(parser),
Expand All @@ -57,7 +57,6 @@ impl Parser{
}



impl Parser {
/// Gets the grammar of the parser.
pub fn grammar(&self) -> &Grammar {
Expand Down Expand Up @@ -97,23 +96,23 @@ impl Parser {

#[cfg(feature = "wasm")]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl Parser{
pub fn first_table_wasm(&self) -> Result<JsValue,JsValue> {
impl Parser {
pub fn first_table_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.first_table)?)
}
pub fn follow_table_wasm(&self) -> Result<JsValue,JsValue> {
pub fn follow_table_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.follow_table)?)
}
pub fn automaton_wasm(&self) -> Result<JsValue,JsValue> {
pub fn automaton_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.automaton)?)
}
pub fn parsing_tables_wasm(&self) -> Result<JsValue,JsValue> {
pub fn parsing_tables_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.parsing_tables)?)
}
pub fn action_table_wasm(&self) -> Result<JsValue,JsValue> {
pub fn action_table_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.parsing_tables.action_table())?)
}
pub fn goto_table_wasm(&self) -> Result<JsValue,JsValue> {
pub fn goto_table_wasm(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.parsing_tables.goto_table())?)
}
}
Expand Down Expand Up @@ -206,7 +205,7 @@ impl Parser {
let trace = serde_wasm_bindgen::to_value(&trace)?;
let tree = serde_wasm_bindgen::to_value(&tree)?;
Ok(vec![trace, tree])
}
},
Err(error) => Err(serde_wasm_bindgen::to_value(&error)?),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ impl<T: AsRef<str>> MockColored for T {
fn bold(&self) -> String {
self.as_ref().to_owned()
}
}
}

0 comments on commit c0c13a3

Please sign in to comment.