Files
developtools_ace-ets2bundle/compiler/syntax_parser/src/syntax_parser.peg
T
houhaoyu 5c91acc6b5 houhaoyu
update code from wagner

Signed-off-by: houhaoyu <houhaoyu@huawei.com>
Change-Id: Id554e2c583d073eaae76b1201e9422fed135c7ae
2021-12-21 17:15:23 +08:00

124 lines
4.0 KiB
Plaintext

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
const parse_extend = require('./parse_extend.js');
const error_otherParsers = [];
let singleError = function(errMessage, line, column) {
this.errMessage = errMessage;
this.errPosition = {
line: line,
column: column
};
};
const collect_extend = {
component: [],
functionName: [],
parameters: []
};
}
start = blocks: block+
{
let newContent = "";
for (let item in blocks) {
if (Array.isArray(blocks[item])) {
newContent += blocks[item].join('');
} else {
newContent += blocks[item];
}
}
return {
content: newContent,
location: location(),
collect_extend: collect_extend,
error_otherParsers: error_otherParsers
};
}
block = extend/(prefix extend?)
prefix = $((!prefix_extend .)+)
extend =
whiteSpace1:whiteSpace prefix_extend:prefix_extend funcBody_extend:funcBody_extend
whiteSpace2: whiteSpace right:'}' whiteSpace3:whiteSpace
{
return whiteSpace1 + prefix_extend + funcBody_extend + whiteSpace2 + right + whiteSpace3;
}
prefix_extend = prefix_extend_one/prefix_extend_another
//match a kind of extend component
prefix_extend_one =
'@Extend' whiteSpace1:whiteSpace '(' component:component ')' whiteSpace2:whiteSpace 'function'
whiteSpace3:whiteSpace function_name:function_name whiteSpace4:whiteSpace '(' parameters:parameters
')' whiteSpace5:whiteSpace left:'{'
{
collect_extend.component.push(component);
collect_extend.functionName.push(function_name);
collect_extend.parameters.push(parameters);
return '@Extend' + whiteSpace1 + whiteSpace2 + 'function' + whiteSpace3 + '__' + component +
'__' + function_name + whiteSpace4 + '(' + parameters + ')' + whiteSpace5 + left + component;
}
//match another kind of extend component
prefix_extend_another =
'@Extend' whiteSpace1:whiteSpace component:component '.' function_name:function_name
whiteSpace2:whiteSpace '(' parameters:parameters ')' whiteSpace3:whiteSpace left:'{'
{
collect_extend.component.push(component);
collect_extend.functionName.push(function_name);
collect_extend.parameters.push(parameters);
return '@Extend' + ' function' + whiteSpace1 + '__' + component + '__' + function_name +
whiteSpace2 + '(' + parameters + ')' + whiteSpace3 + left + component;
}
component = $ [a-zA-Z_]+
function_name = $ (function_name_head function_name_tail)
function_name_head = $ [a-zA-Z_$]+
function_name_tail = $ [a-zA-Z0-9_$]*
parameters = $ ([^()]* item* [^()]*)
//extract Extend internal attribute SyntaxError
funcBody_extend = body:$([^{}]* item* [^{}]*)
{
try {
parse_extend.parse(body);
return body;
} catch (err) {
let countLines = location().end.line - location().start.line;
if (err.location.start.line === 1) {
err.location.start.column += location().start.column;
}
let supportMessage = " Our rule is .attribute(value) , for example: .width(50) . And you'd better have at least one attribute in Extend Component";
error_otherParsers.push(
new singleError(
parse_extend.SyntaxError.buildMessage(err.expected, err.found) + supportMessage,
err.location.start.line + location().start.line - 1,
err.location.start.column
)
);
let placeHolders = '';
while (countLines>0) {
placeHolders += '\r\n';
countLines -= 1;
}
return '()' + placeHolders;
}
}
item = $ (whiteSpace '{' [^{}]* item* [^{}]* '}' whiteSpace)
whiteSpace = $ [ \t\r\n]*