Class: Stone::Grammar

Inherits:
Grammy::Grammar
  • Object
show all
Defined in:
lib/stone/grammar.rb

Constant Summary collapse

ALPHA_IDENTIFIER =

Identifier and operator patterns

/[a-zA-Z_][a-zA-Z0-9_]*[!?]?/
COMPUTED_PROPERTY_ID =
/[A-Z][a-zA-Z0-9_]*@[a-zA-Z_][a-zA-Z0-9_]*[!?]?/
COMPARISON_OPERATOR =
/(==|!=|<=|>=|≠|≤|≥|<|>)/
BOOLEAN_OPERATOR =
/(∧|∨|⊻|¬)/

Instance Method Summary collapse

Instance Method Details

#braces(submatchers) ⇒ Object

Match the passed-in matchers/combinators within curly braces, with whitespace allowed.



67
68
69
# File 'lib/stone/grammar.rb', line 67

def braces(submatchers)
  str("{") + ws? + submatchers + ws? + str("}")
end

#comma_separated(submatchers, allow_trailing: true) ⇒ Object

Match a comma-separated list, optionally allowing a trailing comma.



72
73
74
# File 'lib/stone/grammar.rb', line 72

def comma_separated(submatchers, allow_trailing: true)
  list(submatchers, separated_by: str(","), allow_trailing: allow_trailing)
end

#list(items, separated_by:, allow_trailing: true, allow_repeated_separator: false, min: 0) ⇒ Object

Match a list of matchers/combinators, separated by a specified separator.

  • Supports allowing trailing separator (default: true)
  • Supports allowing repeated consecutive separators (default: false)
  • Supports minimum item count (default: 0)


80
81
82
83
84
85
86
87
88
89
# File 'lib/stone/grammar.rb', line 80

def list(items, separated_by:, allow_trailing: true, allow_repeated_separator: false, min: 0)
  list_repetition = min.zero? ? [0..] : [min..]
  items_with_separators = ws? + items + (separated_by + ws? + items)[0..]
  if allow_trailing
    trailing_repetition = allow_repeated_separator ? [0..] : [0..1]
    (items_with_separators + (separated_by + ws?)[*trailing_repetition])[*list_repetition]
  else
    items_with_separators[*list_repetition]
  end
end

#parens(submatchers) ⇒ Object

Custom Matchers/Combinators Match the passed-in matchers/combinators within parentheses, with whitespace allowed.



62
63
64
# File 'lib/stone/grammar.rb', line 62

def parens(submatchers)
  str("(") + ws? + submatchers + ws? + str(")")
end