Skip to content

Commit

Permalink
lex newline
Browse files Browse the repository at this point in the history
  • Loading branch information
robertDurst committed Jan 6, 2024
1 parent a268dae commit e47f1b1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
14 changes: 13 additions & 1 deletion spec/zodiac/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
end

context 'when happy path' do
it 'lexs newlines' do
input = "\n"
lexer = described_class.new(input)

expected_output = [{ kind: 'NEWLINE', value: "\n" }]

expect(lexer.lex).to eq(expected_output)
end

it 'lexs symbols' do
input = ':[ ]{}@~$!?:='
lexer = described_class.new(input)
Expand Down Expand Up @@ -178,8 +187,11 @@

expected_output = [
{ kind: 'COMMENT', value: '# this is a comment' },
{ kind: 'NEWLINE', value: "\n" },
{ kind: 'COMMENT', value: '# this is another comment' },
{ kind: 'COMMENT', value: '# this is a third comment' }
{ kind: 'NEWLINE', value: "\n" },
{ kind: 'COMMENT', value: '# this is a third comment' },
{ kind: 'NEWLINE', value: "\n" }
]

expect(lexer.lex).to eq(expected_output)
Expand Down
2 changes: 1 addition & 1 deletion spec/zodiac/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
parser = described_class.new('')

actual = parser.parse
expected = { kind: 'PROGRAM', value: [] }
expected = { kind: 'PROGRAM', cmp_stmts: [] }

expect(actual).to eq(expected)
end
Expand Down
16 changes: 14 additions & 2 deletions src/zodiac/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Lexer
def initialize(raw_string)
@input_iterator = StringCharacterIterator.new(raw_string)
@lexer_evaluator_engine = LexerEvaluatorEngine.new(
[comment_lexer, op_assign_lexer, symbol_lexer, identifier_lexer, string_lexer, number_lexer],
[comment_lexer, newline_lexer, op_assign_lexer, symbol_lexer, identifier_lexer, string_lexer, number_lexer],
proc { @input_iterator.iterate }
)
end
Expand All @@ -37,7 +37,6 @@ def lex
private

### Comment lexing ###

def comment_lexer
LexerEvaluator.new('COMMENT', method(:comment?), method(:lex_comment))
end
Expand All @@ -46,6 +45,19 @@ def lex_comment
@input_iterator.take_until(proc { |val| val == "\n" })
end

### Newline lexing ###
def newline_lexer
LexerEvaluator.new('NEWLINE', method(:newline?), method(:lex_newline))
end

def lex_newline
@input_iterator.iterate
end

def newline?(_value)
@input_iterator.peek == "\n"
end

### Operator Assignment lexing ###
def lex_op_assign
@input_iterator.take_until(proc { |val| val == '=' }, after: 1)
Expand Down
6 changes: 6 additions & 0 deletions src/zodiac/parse_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Zodiac
class ParseError < StandardError
end
end
11 changes: 9 additions & 2 deletions src/zodiac/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def parse_program

cmp_stmts << parse_compstmt while @cur_index < @tokens.length

{ kind: 'PROGRAM', value: cmp_stmts }
{ kind: 'PROGRAM', cmp_stmts: }
end

# COMPSTMT : STMT (TERM EXPR)* [TERM]
Expand Down Expand Up @@ -212,7 +212,14 @@ def parse_expr
# TERM : `;'
# | `\n'
def parse_term
{ kind: 'TERM', value: nil }
cur_token = @tokens[@cur_index]

unless cur_token[:kind] == 'NEWLINE' || (cur_token[:kind] == 'SYMBOL' && cur_token[:value] == ';')
raise ParseError,
"Expected a newline or a semicolon. Received #{cur_token[:value]}"
end

{ kind: 'TERM', value: cur_token[:value] }
end

# CALL : FUNCTION
Expand Down

0 comments on commit e47f1b1

Please sign in to comment.