-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ugly first iteration of lexer with tests
- Loading branch information
1 parent
3260fb8
commit bc8dc67
Showing
11 changed files
with
616 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,5 +69,8 @@ DEPENDENCIES | |
rubocop-rspec | ||
simplecov | ||
|
||
RUBY VERSION | ||
ruby 3.2.2p53 | ||
|
||
BUNDLED WITH | ||
2.4.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'simplecov' | ||
SimpleCov.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require './spec/spec_helper' | ||
require './src/zodiac/cli' | ||
|
||
describe Zodiac::CLI do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
# frozen_string_literal: true | ||
|
||
require './spec/spec_helper' | ||
require './src/zodiac/lexer' | ||
|
||
describe Zodiac::Lexer do | ||
describe '#lex' do | ||
context 'when empty input' do | ||
it 'returns an empty array of tokens' do | ||
input = '' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
end | ||
|
||
context 'when invalid input' do | ||
context 'when fails to lex a string' do | ||
it 'raises an error' do | ||
input = '"hello world' | ||
lexer = described_class.new(input) | ||
expect { lexer.lex }.to raise_error(Zodiac::LexError) | ||
end | ||
end | ||
end | ||
|
||
context 'when happy path' do | ||
it 'lexs symbols' do | ||
input = ':[ ]{}@~$!?:=' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [ | ||
{ kind: 'SYMBOL', value: ':' }, | ||
{ kind: 'SYMBOL', value: '[' }, | ||
{ kind: 'SYMBOL', value: ']' }, | ||
{ kind: 'SYMBOL', value: '{' }, | ||
{ kind: 'SYMBOL', value: '}' }, | ||
{ kind: 'SYMBOL', value: '@' }, | ||
{ kind: 'SYMBOL', value: '~' }, | ||
{ kind: 'SYMBOL', value: '$' }, | ||
{ kind: 'SYMBOL', value: '!' }, | ||
{ kind: 'SYMBOL', value: '?' }, | ||
{ kind: 'SYMBOL', value: ':' }, | ||
{ kind: 'SYMBOL', value: '=' } | ||
] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
|
||
it 'lexs whole words' do | ||
input = 'hello WorLD c4k3 _r3d_foo' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [ | ||
{ kind: 'IDENTIFIER', value: 'hello' }, | ||
{ kind: 'IDENTIFIER', value: 'WorLD' }, | ||
{ kind: 'IDENTIFIER', value: 'c4k3' }, | ||
{ kind: 'IDENTIFIER', value: '_r3d_foo' } | ||
] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
|
||
it 'splits words on symbols' do | ||
input = 'hello:world' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [ | ||
{ kind: 'IDENTIFIER', value: 'hello' }, | ||
{ kind: 'SYMBOL', value: ':' }, | ||
{ kind: 'IDENTIFIER', value: 'world' } | ||
] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
|
||
it 'lexs operators' do | ||
input = '+ - * / % ** & | ^ << >> && || @@::..== === =~ +@ -@ [] <=>' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [ | ||
{ kind: 'SYMBOL', value: '+' }, | ||
{ kind: 'SYMBOL', value: '-' }, | ||
{ kind: 'SYMBOL', value: '*' }, | ||
{ kind: 'SYMBOL', value: '/' }, | ||
{ kind: 'SYMBOL', value: '%' }, | ||
{ kind: 'SYMBOL', value: '**' }, | ||
{ kind: 'SYMBOL', value: '&' }, | ||
{ kind: 'SYMBOL', value: '|' }, | ||
{ kind: 'SYMBOL', value: '^' }, | ||
{ kind: 'SYMBOL', value: '<<' }, | ||
{ kind: 'SYMBOL', value: '>>' }, | ||
{ kind: 'SYMBOL', value: '&&' }, | ||
{ kind: 'SYMBOL', value: '||' }, | ||
{ kind: 'SYMBOL', value: '@@' }, | ||
{ kind: 'SYMBOL', value: '::' }, | ||
{ kind: 'SYMBOL', value: '..' }, | ||
{ kind: 'SYMBOL', value: '==' }, | ||
{ kind: 'SYMBOL', value: '===' }, | ||
{ kind: 'SYMBOL', value: '=~' }, | ||
{ kind: 'SYMBOL', value: '+@' }, | ||
{ kind: 'SYMBOL', value: '-@' }, | ||
{ kind: 'SYMBOL', value: '[]' }, | ||
{ kind: 'SYMBOL', value: '<=>' } | ||
] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
|
||
it 'lexs operators with assignment' do | ||
input = '+= -= *= /= %= **= &= |= ^= <<= >>= &&= ||= []= >= <=' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [ | ||
{ kind: 'OP_ASGN', value: '+=' }, | ||
{ kind: 'OP_ASGN', value: '-=' }, | ||
{ kind: 'OP_ASGN', value: '*=' }, | ||
{ kind: 'OP_ASGN', value: '/=' }, | ||
{ kind: 'OP_ASGN', value: '%=' }, | ||
{ kind: 'OP_ASGN', value: '**=' }, | ||
{ kind: 'OP_ASGN', value: '&=' }, | ||
{ kind: 'OP_ASGN', value: '|=' }, | ||
{ kind: 'OP_ASGN', value: '^=' }, | ||
{ kind: 'OP_ASGN', value: '<<=' }, | ||
{ kind: 'OP_ASGN', value: '>>=' }, | ||
{ kind: 'OP_ASGN', value: '&&=' }, | ||
{ kind: 'OP_ASGN', value: '||=' }, | ||
{ kind: 'OP_ASGN', value: '[]=' }, | ||
{ kind: 'OP_ASGN', value: '>=' }, | ||
{ kind: 'OP_ASGN', value: '<=' } | ||
] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
|
||
it 'lexs strings' do | ||
input = '"hello world" \'hello world\' `hello world`' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [ | ||
{ kind: 'STRING', value: '"hello world"' }, | ||
{ kind: 'STRING', value: "'hello world'" }, | ||
{ kind: 'STRING', value: '`hello world`' } | ||
] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
|
||
it 'lexs numbers' do | ||
input = '123 123.456' | ||
lexer = described_class.new(input) | ||
|
||
expected_output = [ | ||
{ kind: 'NUMBER', value: '123' }, | ||
{ kind: 'NUMBER', value: '123.456' } | ||
] | ||
|
||
expect(lexer.lex).to eq(expected_output) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Zodiac | ||
class LexError < StandardError | ||
end | ||
end |
Oops, something went wrong.