From b3c7ed56ee40e2848f120db61c342095edb1a129 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 17 Oct 2014 11:06:38 -0400 Subject: [PATCH] Fixed all pending specs --- spec/lexer_spec.rb | 10 +-------- spec/parser_spec.rb | 26 ----------------------- spec/spec_helper.rb | 50 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/spec/lexer_spec.rb b/spec/lexer_spec.rb index 10c822f..7857634 100644 --- a/spec/lexer_spec.rb +++ b/spec/lexer_spec.rb @@ -1,11 +1,4 @@ -require 'open3' - -def lexer(input) - out, err, status = Open3.capture3('./build/bin/sclpl-test', '--tokens', :stdin_data => input) - raise "Lexer command returned non-zero status" unless status.success? - raise err unless err == "" - out.gsub(//,'\1').split -end +require 'spec_helper' describe "lexer" do context "punctuation" do @@ -156,7 +149,6 @@ describe "lexer" do end it "should recognize a string that spans lines" do - pending "S-Expression parser is stupid. fix it." expect(lexer("\"a\nb\"")).to eq ["T_STRING:\"a\nb\""] end end diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 858c1db..dda0c4d 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -1,31 +1,5 @@ require 'open3' -def re_structure( token_array, offset = 0 ) - struct = [] - while( offset < token_array.length ) - if(token_array[offset] == "(") - # Multiple assignment from the array that re_structure() returns - offset, tmp_array = re_structure(token_array, offset + 1) - struct << tmp_array - elsif(token_array[offset] == ")") - break - else - struct << token_array[offset] - end - offset += 1 - end - return [offset, struct] -end - -def ast(input) - out, err, status = Open3.capture3('./build/bin/sclpl-test', '--ast', :stdin_data => input) - raise err unless err == "" - raise "Parser command returned non-zero status" unless status.success? - out.gsub!(/([()])|tree/,' \1 ') - off, expr = re_structure(out.split) - expr -end - describe "sclpl grammar" do context "requires" do it "should parse a require statement" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ce150c6..521a463 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,51 @@ +require 'open3' -module TestUtils -def self.exec_cmd(cmd, input) - out, err, status = Open3.capture3(*cmd, :stdin_data => input) +def lexer(input) + out, err, status = Open3.capture3('./build/bin/sclpl-test', '--tokens', :stdin_data => input) + raise "Lexer command returned non-zero status" unless status.success? + raise err unless err == "" + out.scan(/^(T_[A-Z]+(:("[^"]*"|[^\n]+))?)/m).map {|m| m[0] } end + +def re_structure( token_array, offset = 0 ) + struct = [] + while( offset < token_array.length ) + if(token_array[offset] == "(") + # Multiple assignment from the array that re_structure() returns + offset, tmp_array = re_structure(token_array, offset + 1) + struct << tmp_array + elsif(token_array[offset] == ")") + break + else + struct << token_array[offset] + end + offset += 1 + end + return [offset, struct] +end + +def ast(input) + out, err, status = Open3.capture3('./build/bin/sclpl-test', '--ast', :stdin_data => input) + raise err unless err == "" + raise "Parser command returned non-zero status" unless status.success? + # Prep the parens for reading + out.gsub!(/([()])|tree/,' \1 ') + # Replace string literals so we can tokenize on spaces + strings = [] + out.gsub!(/"[^\"]*"/) do |m| + strings << m + '$_$_STRING_$_$' + end + # Put the strings back in after splitting + tokens = out.split.map do |tok| + if tok.end_with? '$_$_STRING_$_$' + tok.gsub('$_$_STRING_$_$', strings.shift) + else + tok + end + end + # Build the tree structure and return the result + off, expr = re_structure(tokens) + expr end -- 2.52.0