Class: Romanesco::Tokeniser
- Inherits:
-
Object
- Object
- Romanesco::Tokeniser
- Defined in:
- lib/romanesco/tokeniser.rb
Constant Summary collapse
- TOKEN_TYPES =
[ {name: 'addition', regex: /^\+/, token_class: AdditionToken}, {name: 'multiplication', regex: /^\*/, token_class: MultiplicationToken}, {name: 'subtraction', regex: /^\-/, token_class: SubtractionToken}, {name: 'division', regex: /^\//, token_class: DivisionToken}, {name: 'open parenthesis', regex: /^\(/, token_class: OpenParenthesisToken}, {name: 'close parenthesis', regex: /^\)/, token_class: CloseParenthesisToken}, {name: 'constant', regex: /^[0-9]*\.?[0-9]+/, token_class: ConstantToken}, {name: 'string', regex: /^[a-zA-Z_]+/, token_class: VariableToken} ]
Instance Method Summary collapse
Instance Method Details
#tokenise(raw_expression) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/romanesco/tokeniser.rb', line 17 def tokenise(raw_expression) expression = remove_commas_and_whitespace(raw_expression) tokens = [] while expression.length > 0 token_count = tokens.count TOKEN_TYPES.each do |type| if expression =~ type[:regex] token_string = expression.slice!(type[:regex]) tokens << type[:token_class].send(:new, token_string) end end raise InvalidExpressionError.new("Expression error starting at #{expression}") if tokens.size == token_count end tokens end |