From cad8a88b119a0127a19a0cfb00b90314fc743f8d Mon Sep 17 00:00:00 2001 From: inglorion Date: Sun, 14 Jul 2013 20:17:57 -0700 Subject: [PATCH] fixed errors and help tests --- lib/voodoo/parser.rb | 18 ++++++++++++++++-- lib/voodoo/validator.rb | 5 +++-- test/errors.err | 2 +- test/help.out | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/voodoo/parser.rb b/lib/voodoo/parser.rb index 50c00f5..4f070e2 100644 --- a/lib/voodoo/parser.rb +++ b/lib/voodoo/parser.rb @@ -174,6 +174,9 @@ module Voodoo else kind_text = kind.to_s end + # Groups are allowed to contain top-level statements. + # All other kinds aren't. + top_level = kind == :group done = false until done begin @@ -193,10 +196,21 @@ module Voodoo else # Should be a normal statement. Validate it, then add it to body begin - Validator.validate_statement statement + if top_level + Validator.validate_top_level statement + else + Validator.validate_statement statement + end body << statement rescue Validator::ValidationError => e - parse_error e.message + magic_word = statement[0] + if !top_level && + Validator::TOP_LEVELS.member?(magic_word) && + !Validator::STATEMENTS.member?(magic_word) + parse_error "#{magic_word} is only allowed at top-level" + else + parse_error e.message + end end end end diff --git a/lib/voodoo/validator.rb b/lib/voodoo/validator.rb index 29e06d9..ffaf8ea 100644 --- a/lib/voodoo/validator.rb +++ b/lib/voodoo/validator.rb @@ -15,14 +15,15 @@ module Voodoo EXPRS = BINOPS + UNOPS + VARARG_EXPRS # Symbols that are a valid start of a statement. - STATEMENTS = [:byte, :block, :call, :goto, :group, :ifeq, :ifge, + STATEMENTS = [:byte, :block, :call, :goto, :ifeq, :ifge, :ifgt, :ifle, :iflt, :ifne, :label, :let, :return, :'restore-frame', :'restore-locals', :'save-frame', :'save-frame-and-locals', :'save-locals', :set, :'set-byte', :'set-word', :string, :'tail-call', :word] # Symbols that are valid at top-level. - TOP_LEVELS = [:align, :export, :function, :import, :section] + STATEMENTS + TOP_LEVELS = [:align, :export, :function, :group, :import, :section] + + STATEMENTS # Maps indices 0, 1, 2 to English words. NTH = ['First', 'Second', 'Third'] diff --git a/test/errors.err b/test/errors.err index cdfc5fb..ebea029 100644 --- a/test/errors.err +++ b/test/errors.err @@ -20,7 +20,7 @@ errors.voo:16: let is not allowed inside iflt let x add 2 3 end if -errors.voo:22: Function definitions are only allowed at top-level +errors.voo:22: function is only allowed at top-level function bar x return x diff --git a/test/help.out b/test/help.out index 0248544..af6a54a 100644 --- a/test/help.out +++ b/test/help.out @@ -22,7 +22,7 @@ Valid options are: supported output formats for architecture. --features - Lists the features supported by this implementation. The features + List the features supported by this implementation. The features are listed in the format , ordered by feature name. The program exits after printing the list of features; no compilation is performed. -- 2.11.4.GIT