Class: Yadriggy::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/yadriggy/syntax.rb

Overview

Syntax checker.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast) ⇒ Syntax

Returns a new instance of Syntax.

Parameters:

  • ast (ASTree)

    the grammar rules.



62
63
64
65
66
67
68
# File 'lib/yadriggy/syntax.rb', line 62

def initialize(ast)
  @error_loc = nil
  @error_msg = nil
  @debug = false
  @hash = {}
  update_hash(ast.tree.body)
end

Instance Attribute Details

#debug=(value) ⇒ Object (writeonly)

debugging mode is on if true.



42
43
44
# File 'lib/yadriggy/syntax.rb', line 42

def debug=(value)
  @debug = value
end

#hashHash (readonly)

Returns the grammar rules.

Returns:

  • (Hash)

    the grammar rules.



39
40
41
# File 'lib/yadriggy/syntax.rb', line 39

def hash
  @hash
end

Class Method Details

.check_syntax(block) ⇒ Boolean

Checks the syntax of the grammar rules.

Parameters:

  • block (ASTnode)

    the grammar rules.

Returns:

  • (Boolean)

    false if a syntax error is found in the given grammar rules.



51
52
53
# File 'lib/yadriggy/syntax.rb', line 51

def self.check_syntax(block)
  @syntax.nil? || @syntax.check(block)
end

.last_errorString

Returns the error message for the grammar rule last checked.

Returns:

  • (String)

    the error message for the grammar rule last checked.



57
58
59
# File 'lib/yadriggy/syntax.rb', line 57

def self.last_error
  @syntax.error
end

.ruby_syntaxSyntax

Defines Ruby syntax and returns its Syntax object.

Returns:

  • (Syntax)

    the Ruby syntax.



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/yadriggy/syntax.rb', line 469

def self.ruby_syntax
  Yadriggy.define_syntax do
    expr  <= Name | Number | Super | Binary | Unary | SymbolLiteral |
            ConstPathRef | StringLiteral | StringInterpolation |
            ArrayLiteral | Paren | Call | ArrayRef | HashLiteral |
            Return | ForLoop | Loop | Conditional | Break |
            Lambda | BeginEnd | Def | ModuleDef
    exprs <= Exprs | expr

    Name <= { name: String }
    Number     <= { value: Numeric }
    Super      <= {}
    Identifier <= Name
    SymbolLiteral    <= { name: String }
    VariableCall     <= Name
    InstanceVariable <= Name
    GlobalVariable   <= Name
    Label      <= Name
    Reserved   <= Name
    Const      <= Name
    Binary     <= { left: expr, op: Symbol, right: expr }
    ArrayRef   <= { array: expr, indexes: [ expr ] }
    ArrayRefField  <= ArrayRef
    Assign     <= { left: [expr] | expr, op: Symbol,
                    right: [expr] | expr }
    Dots       <= Binary
    Unary      <= { op: Symbol, operand: expr }
    ConstPathRef   <= { scope: (ConstPathRef | Const), name: Const }
    ConstPathField <= ConstPathRef
    StringLiteral  <= { value: String }
    StringInterpolation <= { contents: [ exprs ] }
    ArrayLiteral   <= { elements: [ expr ] }
    Paren      <= { expression: expr }
    HashLiteral    <= { pairs: [ expr * expr ] }
    Return     <= { values: [ expr ] }
    ForLoop    <= {vars: [ Identifier ], set: expr, body: exprs }
    Loop       <= { op: Symbol, cond: expr, body: exprs }
    Conditional <= { op: Symbol, cond: expr, then: exprs,
                     all_elsif: [expr * exprs], else: (exprs) }
    Parameters <= { params: [ Identifier ],
                   optionals: [ Identifier * expr ],
                   rest_of_params: (Identifier),
                   params_after_rest: [ Identifier ],
                   keywords: [ Label * expr ],
                   rest_of_keywords: (Identifier),
                   block_param: (Identifier) }
    Block      <= Parameters + { body: exprs }
    Lambda     <= Block
    Call       <= { receiver: (expr), op: (Symbol), name: (Identifier | Const),
                    args: [ expr ], block_arg: (expr), block: (Block) }
    Command    <= Call
    Exprs      <= { expressions: [ expr ] }
    Rescue     <= { types: [ Const | ConstPathRef ],
                    parameter: (Identifier),
                    body: (exprs), nested_rescue: (Rescue),
                    else: (exprs), ensure: (exprs) }
    BeginEnd   <= { body: exprs, rescue: (Rescue) }
    Def        <= Parameters +
                  { singular: (expr), name: (Identifier | Const), body: exprs,
                    rescue: (Rescue) }
    ModuleDef  <= { name: Const | ConstPathRef, body: exprs,
                    rescue: (Rescue) }
    ClassDef   <= ModuleDef +
                  { superclass: (Const | ConstPathRef) }
    SingularClassDef <= { name: expr, body: exprs,
                    rescue: (Rescue) }
    Program    <= { elements: exprs }
  end
end

Instance Method Details

#add_rules(syntax = nil) { ... } ⇒ Object

Adds rules.

Parameters:

  • syntax (Syntax) (defaults to: nil)

    the rules of this given syntax are added. This parameter is optional.

Yields:

  • the rules in the block are added. The block is optional.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/yadriggy/syntax.rb', line 91

def add_rules(syntax=nil, &block)
  if syntax.is_a?(Syntax)
    syntax.hash.each do |k,v|
      @hash[k] = v
    end
  elsif block.is_a?(Proc)
    ast = Yadriggy::reify(block)
    if Syntax.check_syntax(ast.tree)
      update_hash(ast.tree.body)
    else
      raise Syntax.last_error
    end
  end
end

#check(tree) ⇒ Boolean

Checks the syntax of the given AST.

Parameters:

Returns:

  • (Boolean)

    true when the given AST is syntactically correct.



120
121
122
123
124
125
126
127
128
# File 'lib/yadriggy/syntax.rb', line 120

def check(tree)
  error_cleared!
  expr = find_hash_entry(tree.class)
  if expr
    check_expr(expr, tree, false) || error_found!(tree, tree)
  else
    error_found!(tree, tree, "no rule for #{tree.class}")
  end
end

#check_error(astree) ⇒ void

This method returns an undefined value.

Checks the syntax of the given AST and raise an error if a syntax error is found.

Parameters:

Raises:



112
113
114
# File 'lib/yadriggy/syntax.rb', line 112

def check_error(astree)
  raise_error unless check(astree.is_a?(ASTree) ? astree.tree : astree)
end

#check_usertype(user_type, tree) ⇒ Boolean

Checks whether the given AST matches the grammar rule for the given user type.

Parameters:

  • user_type (String|Symbol)

    the name of the user type.

  • tree (ASTnode)

    the AST.

Returns:

  • (Boolean)

    true if the given AST matches.



136
137
138
139
# File 'lib/yadriggy/syntax.rb', line 136

def check_usertype(user_type, tree)
  error_cleared!
  check_rule_usertype(user_type.to_s, tree, false)
end

#errorString

Returns an error message.

Returns:

  • (String)

    an error message when the last invocation of #check returns false.



144
145
146
147
148
149
150
# File 'lib/yadriggy/syntax.rb', line 144

def error
  if @error_loc.nil? && @error_msg.nil?
    ''
  else
    "#{@error_loc} DSL syntax error#{@error_msg}"
  end
end

#raise_errorObject

Raises a syntax-error exception.

Raises:



154
155
156
# File 'lib/yadriggy/syntax.rb', line 154

def raise_error
  raise SyntaxError.new(error)
end