1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16{ 17 const parse_extend = require('./parse_extend.js'); 18 19 const error_otherParsers = []; 20 let singleError = function(errMessage, line, column) { 21 this.errMessage = errMessage; 22 this.errPosition = { 23 line: line, 24 column: column 25 }; 26 }; 27 const collect_extend = { 28 component: [], 29 functionName: [], 30 parameters: [] 31 }; 32} 33 34start = blocks: block+ 35{ 36 let newContent = ""; 37 for (let item in blocks) { 38 if (Array.isArray(blocks[item])) { 39 newContent += blocks[item].join(''); 40 } else { 41 newContent += blocks[item]; 42 } 43 } 44 return { 45 content: newContent, 46 location: location(), 47 collect_extend: collect_extend, 48 error_otherParsers: error_otherParsers 49 }; 50} 51 52block = extend/(prefix extend?) 53prefix = $((!prefix_extend .)+) 54extend = 55 whiteSpace1:whiteSpace prefix_extend:prefix_extend funcBody_extend:funcBody_extend 56 whiteSpace2: whiteSpace right:'}' whiteSpace3:whiteSpace 57{ 58 return whiteSpace1 + prefix_extend + funcBody_extend + whiteSpace2 + right + whiteSpace3; 59} 60 61prefix_extend = prefix_extend_one/prefix_extend_another 62 63//match a kind of extend component 64prefix_extend_one = 65 '@Extend' whiteSpace1:whiteSpace '(' component:component ')' whiteSpace2:whiteSpace 'function' 66 whiteSpace3:whiteSpace function_name:function_name whiteSpace4:whiteSpace '(' parameters:parameters 67 ')' whiteSpace5:whiteSpace left:'{' 68{ 69 collect_extend.component.push(component); 70 collect_extend.functionName.push(function_name); 71 collect_extend.parameters.push(parameters); 72 return '@Extend' + whiteSpace1 + whiteSpace2 + 'function' + whiteSpace3 + '__' + component + 73 '__' + function_name + whiteSpace4 + '(' + parameters + ')' + whiteSpace5 + left + component; 74} 75 76//match another kind of extend component 77prefix_extend_another = 78 '@Extend' whiteSpace1:whiteSpace component:component '.' function_name:function_name 79 whiteSpace2:whiteSpace '(' parameters:parameters ')' whiteSpace3:whiteSpace left:'{' 80{ 81 collect_extend.component.push(component); 82 collect_extend.functionName.push(function_name); 83 collect_extend.parameters.push(parameters); 84 return '@Extend' + ' function' + whiteSpace1 + '__' + component + '__' + function_name + 85 whiteSpace2 + '(' + parameters + ')' + whiteSpace3 + left + component; 86} 87 88component = $ [a-zA-Z_]+ 89function_name = $ (function_name_head function_name_tail) 90function_name_head = $ [a-zA-Z_$]+ 91function_name_tail = $ [a-zA-Z0-9_$]* 92parameters = $ ([^()]* item* [^()]*) 93 94//extract Extend internal attribute SyntaxError 95funcBody_extend = body:$([^{}]* item* [^{}]*) 96{ 97 try { 98 parse_extend.parse(body); 99 return body; 100 } catch (err) { 101 let countLines = location().end.line - location().start.line; 102 if (err.location.start.line === 1) { 103 err.location.start.column += location().start.column; 104 } 105 let supportMessage = " Our rule is .attribute(value) , for example: .width(50) . And you'd better have at least one attribute in Extend Component"; 106 error_otherParsers.push( 107 new singleError( 108 parse_extend.SyntaxError.buildMessage(err.expected, err.found) + supportMessage, 109 err.location.start.line + location().start.line - 1, 110 err.location.start.column 111 ) 112 ); 113 let placeHolders = ''; 114 while (countLines>0) { 115 placeHolders += '\r\n'; 116 countLines -= 1; 117 } 118 return '()' + placeHolders; 119 } 120} 121 122item = $ (whiteSpace '{' [^{}]* item* [^{}]* '}' whiteSpace) 123whiteSpace = $ [ \t\r\n]* 124