Class: JsDuck::TypeParser
- Inherits:
-
Object
- Object
- JsDuck::TypeParser
- Defined in:
- lib/jsduck/type_parser.rb
Overview
Validates the syntax of type definitions.
The parser supports a combination of two syntaxes:
-
Traditional type expressions found in ExtJS code:
SomeType Name.spaced.Type Number[] String/RegExp Type...
-
Google Closure Compiler Type Expressions:
boolean Window goog.ui.Menu Array.<string> Object.<string, number> {myNum: 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
-
#error ⇒ Object
readonly
Allows to check the type of error that was encountered.
-
#out ⇒ Object
readonly
When parsing was successful, then contains the output HTML - the input type-definition with types themselves replaced with links.
Instance Method Summary collapse
-
#initialize(relations = {}, formatter = {}) ⇒ TypeParser
constructor
Initializes the parser with hash of valid type names and doc_formatter.
-
#parse(str) ⇒ Object
Parses the type definition.
Constructor Details
#initialize(relations = {}, formatter = {}) ⇒ TypeParser
Initializes the parser with hash of valid type names and doc_formatter.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jsduck/type_parser.rb', line 52 def initialize(relations={}, formatter={}) @relations = 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
45 46 47 |
# File 'lib/jsduck/type_parser.rb', line 45 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.
49 50 51 |
# File 'lib/jsduck/type_parser.rb', line 49 def out @out end |
Instance Method Details
#parse(str) ⇒ Object
Parses the type definition
<type> ::= <alteration-type>
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/jsduck/type_parser.rb', line 69 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 |