UNPKG

3.09 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * @export @class Parser
5 */
6const Parser = require( "./boilerplate.view.parser.constructor" );
7
8/**
9 * @member addName
10 * When you need a variable name, this method helps you to not
11 * duplicate its name.
12 * @param {string} name - Name to add to the list of already used names.
13 * @return {boolean} `true` if the name did not exist until now.
14 */
15/**
16 * @member getFreeName
17 * @param {string} name - Prefix of the name you want to get.
18 * @return {string} A name with a number appened to it.
19 * @example
20 * var p = new Parser();
21 * p.getFreeName("foo"); // -> "foo1"
22 * p.getFreeName("foo"); // -> "foo2"
23 * p.getFreeName("bar"); // -> "bar1"
24 * p.getFreeName("foo"); // -> "foo3"
25 */
26require( "./boilerplate.view.parser.names" )( Parser );
27
28/**
29 * @param {object} xjs -
30 */
31Parser.prototype.parse = parser_parse;
32
33/**
34 * @param {object} attribs - for example `{ is-valid:{boolean false}, ... }`.
35 */
36Parser.prototype.parseViewAttribs = parser_parseViewAttribs;
37
38/**
39 * @member parseConverter
40 * @param {object} type
41 * `converter: string`
42 * `converter: {float() 0}`
43 * `converter: {boolean2string() CORRECT WRONG}`
44 * `converter: {Behind myConverter}`
45 * `converter: {| neg abs [integer 0]}`
46 */
47require( "./boilerplate.view.parser.parseConverter" )( Parser );
48
49module.exports = Parser;
50
51
52const Util = require( "./boilerplate.util" );
53
54const RX_VIEW_ATTRIB = /^[a-z](-[a-z0-9]+)*$/;
55
56
57function parser_parse( xjs ) {
58 try {
59 throw Error( "Not implemented!" );
60 } catch ( ex ) {
61 Util.throwError( "Error in module `boilerplate.view.parser`", ex );
62 }
63}
64
65function parser_parseViewAttribs( attribs ) {
66 try {
67 var attribName, attribValue;
68 for ( attribName in attribs ) {
69 attribValue = attribs[ attribName ];
70 parser_parseViewAttribs_parseAttrib.call( this, attribName, attribValue );
71 }
72 } catch ( ex ) {
73 Util.throwError( "Error while parsing `view.attribs`", ex );
74 }
75}
76
77function parser_parseViewAttribs_parseAttrib( attribName, attribValue ) {
78 parser_parseViewAttribs_parseAttrib_check( attribName, attribValue );
79
80 try {
81 var type = attribValue[ '0' ].toLowerCase();
82 if ( CONVERTERS.indexOf( type ) > -1 )
83 return parser_parseViewAttribs_parseAttrib_simple.call( this, type );
84 } catch ( ex ) {
85 Util.throwError( "Error while parsing attribute `" + attribName + "`", ex );
86 }
87}
88
89function parser_parseViewAttribs_parseAttrib_simple( type ) {
90 var varName = this.parseConverter( type );
91}
92
93function parser_parseViewAttribs_parseAttrib_check( attribName, attribValue ) {
94 if ( !RX_VIEW_ATTRIB.test( attribName ) ) {
95 throw "`" + attribName +
96 "` is not a valid attribute name. Examples of valid names are: `x`, `orientation`, `is-enabled`.";
97 }
98 if ( !Util.isSpecial( attribValue ) && !Array.isArray( attribValue[ '0' ] ) ) {
99 throw "`" + attribName + "` must be a special object";
100 }
101}
\No newline at end of file