Skip to content
This repository
Browse code

Factor apart validate_module_body from validate_class_body

Originally named validate_function_body (which is completely wrong),
these functions check what expresions are used in module and class
bodies.

Eventually all expressions will be allowed, but for now modules can
only contain functions and classes can only contain class methods
and instances methods (presently represented as 'functions' in the
Reia parse tree)
  • Loading branch information...
commit 2b308710abd010b45fb7d69332af87e3945a7458 1 parent 310c525
authored

Showing 1 changed file with 21 additions and 9 deletions. Show diff stats Hide diff stats

  1. 30  src/compiler/reia_parse.yrl
30  src/compiler/reia_parse.yrl
@@ -312,7 +312,7 @@ module_decl -> module module_name eol functions 'end' :
312 312
   #module{
313 313
     line  = ?line('$1'), 
314 314
     name  = element(3, '$2'), 
315  
-    exprs = begin validate_function_body('$4'), '$4' end
  315
+    exprs = begin validate_module_body('$4'), '$4' end
316 316
   }.
317 317
   
318 318
 %% Functions
@@ -343,14 +343,14 @@ class_decl -> class module_name methods 'end' :
343 343
   #class{
344 344
     line  = ?line('$1'), 
345 345
     name  = ?identifier_name('$2'),
346  
-    exprs = begin validate_function_body('$3'), '$3' end
  346
+    exprs = begin validate_class_body('$3'), '$3' end
347 347
   }.
348 348
 class_decl -> class module_name '<' module_name methods 'end' : 
349 349
   #class{
350 350
     line   = ?line('$1'), 
351 351
     name   = ?identifier_name('$2'),
352 352
     parent = ?identifier_name('$4'),
353  
-    exprs  = begin validate_function_body('$5'), '$5' end
  353
+    exprs  = begin validate_class_body('$5'), '$5' end
354 354
   }.
355 355
   
356 356
 %% Methods
@@ -363,6 +363,7 @@ methods -> method eol methods : ['$1'|'$3'].
363 363
 %% Method declarations
364 364
 method -> function : '$1'.
365 365
 method -> class_method : '$1'.
  366
+method -> expr : '$1'.
366 367
 
367 368
 %% Class methods
368 369
 class_method -> def self '.' function_name eol body 'end' : 
@@ -836,13 +837,24 @@ string(String) ->
836 837
     Error
837 838
   end.
838 839
   
839  
-%% Ensure a given function body contains only function defs
840  
-validate_function_body([]) -> ok;
841  
-validate_function_body([#function{}|Exprs]) ->
842  
-  validate_function_body(Exprs);
843  
-validate_function_body([Expr|_]) ->
844  
-  throw({error, {element(2, Expr), "Arbitrary expressions not allowed in class/module bodies"}}).
  840
+%% Ensure a given module body contains only function defs
  841
+validate_module_body([]) -> ok;
  842
+validate_module_body([#function{}|Exprs]) ->
  843
+  validate_module_body(Exprs);
  844
+validate_module_body([Expr|_]) ->
  845
+  Line = element(2, Expr),
  846
+  reia:throw('SyntaxError', Line, "Arbitrary expressions not allowed in module bodies").
845 847
   
  848
+%% Ensure a given class body contains only method defs
  849
+validate_class_body([]) -> ok;
  850
+validate_class_body([#class_method{}|Exprs]) ->
  851
+  validate_class_body(Exprs);
  852
+validate_class_body([#function{}|Exprs]) ->
  853
+  validate_class_body(Exprs);
  854
+validate_class_body([Expr|_]) ->
  855
+  Line = element(2, Expr),
  856
+  reia:throw('SyntaxError', Line, "Arbitrary expressions not allowed in class bodies").
  857
+    
846 858
 %% Interpolate strings, parsing the contents of #{...} tags
847 859
 interpolate_string(#string{line=Line, characters=Chars}) ->
848 860
   interpolate_string(Chars, Line, [], []).

0 notes on commit 2b30871

Please sign in to comment.
Something went wrong with that request. Please try again.