Class: JsDuck::TypeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/type_parser.rb

Overview

Validates the syntax of type definitions.

The parser supports a combination of two syntaxes:

  1. Traditional type expressions found in ExtJS code:

    "string" 3.14 SomeType Name.spaced.Type Number String/RegExp Type...

  2. Google Closure Compiler Type Expressions:

    boolean Window goog.ui.Menu

    Array. Object.<string, number>

    number, myObject

    (number|boolean) ?number !Object ...number *

    function(string, boolean): number function(new:goog.ui.Menu, string) function(this:goog.ui.Menu, string) function(?string=, number=) function(string, ...[number])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter) ⇒ TypeParser

Initializes the parser with a Format::Doc instance.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jsduck/type_parser.rb', line 54

def initialize(formatter)
  @relations = formatter.relations
  @formatter = formatter
  @primitives = {
    "boolean" => "Boolean",
    "number" => "Number",
    "string" => "String",
    "null" => "null",
    "undefined" => "undefined",
    "void" => "void",
  }
end

Instance Attribute Details

#error ⇒ Object (readonly)

Allows to check the type of error that was encountered. It will be either of the two:

  • :syntax - type definition syntax is incorrect
  • :name - one of the names of the types is unknown


47
48
49
# File 'lib/jsduck/type_parser.rb', line 47

def error
  @error
end

#out ⇒ Object (readonly)

When parsing was successful, then contains the output HTML - the input type-definition with types themselves replaced with links.



51
52
53
# File 'lib/jsduck/type_parser.rb', line 51

def out
  @out
end

Instance Method Details

#parse(str) ⇒ Object

Parses the type definition

<type> ::= <alteration-type>


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jsduck/type_parser.rb', line 71

def parse(str)
  @input = StringScanner.new(str)
  @error = :syntax
  @out = []

  # Return immediately if the base type doesn't match
  return false unless alteration_type

  # Concatenate all output
  @out = @out.join

  # Success if we have reached the end of input
  return @input.eos?
end