From 84f98ee53daf77927acfce1c0fb447215e1d765c Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Tue, 12 Oct 2021 10:42:45 +0800 Subject: [PATCH 01/12] houhaoyu@huawei.com import pegjs and verify the correctness of extend Signed-off-by: houhaoyu Change-Id: I5f41f2e7d7c222b27dd0d7ccf4818c26d8d00299 --- compiler/build_peg.js | 24 +++++ compiler/package-lock.json | 5 + compiler/package.json | 3 +- compiler/peg_parser/src/index.peg | 120 +++++++++++++++++++++++ compiler/peg_parser/src/parse_extend.peg | 31 ++++++ compiler/src/pre_process.ts | 10 +- compiler/src/validate_ui_syntax.ts | 64 ++++++++---- 7 files changed, 233 insertions(+), 24 deletions(-) create mode 100644 compiler/build_peg.js create mode 100644 compiler/peg_parser/src/index.peg create mode 100644 compiler/peg_parser/src/parse_extend.peg diff --git a/compiler/build_peg.js b/compiler/build_peg.js new file mode 100644 index 000000000..42183ebfa --- /dev/null +++ b/compiler/build_peg.js @@ -0,0 +1,24 @@ +const util = require('util'); +const child_process = require('child_process'); +const exec = util.promisify(child_process.exec); +const fs = require('fs'); +const path = require('path'); +const readDir = fs.readdirSync('./peg_parser/src/'); +const catalog = fs.readdirSync('./peg_parser/'); + +if (catalog.indexOf('dist')>-1) { + exec('rm -rf peg_parser/dist'); +} + +exec('mkdir peg_parser/dist') + +const pegTransJs = async function () { + if (readDir.length) { + for (let item of readDir) { + let name = path.basename(item, '.peg'); + await exec('pegjs -o peg_parser/dist/' + name + '.js peg_parser/src/' + item); + } + } +} + +pegTransJs(); \ No newline at end of file diff --git a/compiler/package-lock.json b/compiler/package-lock.json index de1d8506d..7fa081a90 100644 --- a/compiler/package-lock.json +++ b/compiler/package-lock.json @@ -4295,6 +4295,11 @@ "integrity": "sha1-hTTnenfOesWiUS6iHg/bj89sPY0=", "dev": true }, + "pegjs": { + "version": "0.10.0", + "resolved": "https://registry.npm.taobao.org/pegjs/download/pegjs-0.10.0.tgz", + "integrity": "sha1-z4uvrm7d/0tafvsYUmnqr0YQ3b0=" + }, "picomatch": { "version": "2.3.0", "resolved": "https://registry.nlark.com/picomatch/download/picomatch-2.3.0.tgz", diff --git a/compiler/package.json b/compiler/package.json index 2cc4470f9..509f94b28 100644 --- a/compiler/package.json +++ b/compiler/package.json @@ -10,7 +10,7 @@ ], "scripts": { "lint": "eslint --fix ./src --ext .ts", - "build": "npm run generateDeclarations && ./node_modules/.bin/babel ./src --out-dir lib --extensions .ts", + "build": "node build_peg.js && npm run generateDeclarations && ./node_modules/.bin/babel ./src --out-dir lib --extensions .ts", "create": "node ./lib/create.js --env projectName", "compile": "webpack --config webpack.config.js --env buildMode=debug projectName", "test": "npm run build && mocha -r ts-node/register test/test.ts", @@ -36,6 +36,7 @@ "copy-webpack-plugin": "^8.1.0", "deccjsunit": "latest", "log4js": "^6.3.0", + "pegjs": "^0.10.0", "ts-loader": "^8.0.12", "typescript": "^4.1.3", "webpack": "^5.48.0", diff --git a/compiler/peg_parser/src/index.peg b/compiler/peg_parser/src/index.peg new file mode 100644 index 000000000..c06d8ab4b --- /dev/null +++ b/compiler/peg_parser/src/index.peg @@ -0,0 +1,120 @@ +/* + * 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') + let error_otherParsers = [] + let singleError = function(errMessage, line, column) { + this.errMessage = errMessage; + this.errPosition = { + line: line, + column: column + } + } + let collect_extend = { + component: [], + functionName: [], + parameters: [] + } +} + +start = blocks: block+ { + return { + content: blocks.join(''), + 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 { + let functionBody = body.join('') + parse_extend.parse(functionBody) + return functionBody + } 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]* \ No newline at end of file diff --git a/compiler/peg_parser/src/parse_extend.peg b/compiler/peg_parser/src/parse_extend.peg new file mode 100644 index 000000000..8dc9aae08 --- /dev/null +++ b/compiler/peg_parser/src/parse_extend.peg @@ -0,0 +1,31 @@ +/* + * 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. + */ + +start = extendAttributes:extendAttribute+ +{ + return {location: location()} +} + +extendAttribute = whiteSpace '.' whiteSpace attribute item + +attribute = attr_head attr_tail + +attr_head = $ [a-zA-Z_$]+ + +attr_tail = $ [a-zA-Z0-9_$]* + +item = whiteSpace '(' [^()]* item* [^()]* ')' whiteSpace + +whiteSpace = [ \t\r\n]* \ No newline at end of file diff --git a/compiler/src/pre_process.ts b/compiler/src/pre_process.ts index 752cd1242..3dbfa1e4f 100644 --- a/compiler/src/pre_process.ts +++ b/compiler/src/pre_process.ts @@ -14,6 +14,7 @@ */ import { + afterExtendCheck, sourceReplace, validateUISyntax, processSystemApi @@ -27,13 +28,14 @@ import { BUILD_ON } from './pre_define'; function preProcess(source: string): string { process.env.compiler = BUILD_ON; if (/\.ets$/.test(this.resourcePath)) { - const newContent: string = sourceReplace(source); - const log: LogInfo[] = - validateUISyntax(source, newContent, this.resourcePath, this.resourceQuery); + const result: afterExtendCheck = sourceReplace(source, this.resourcePath) + const content: string = result.content + const log: LogInfo[] = result.log + log.concat(validateUISyntax(source, content, this.resourcePath, this.resourceQuery)); if (log.length) { emitLogInfo(this, log); } - return newContent; + return content; } else { return processSystemApi(source); } diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 94838aba7..614f6797b 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -15,6 +15,7 @@ import ts from 'typescript'; import path from 'path'; +const parser = require('../peg_parser/dist/index.js') import { INNER_COMPONENT_DECORATORS, @@ -665,9 +666,14 @@ function collectionStates(decorator: string, name: string, states: Set, } } -export function sourceReplace(source: string): string { - let content: string = source; +export interface afterExtendCheck { + content: string, + log: LogInfo[] +} +export function sourceReplace(source: string, sourcePath: string): afterExtendCheck { + let content: string = source; + const log: LogInfo[] = []; // replace struct->class content = content.replace( new RegExp('\\b' + STRUCT + '\\b.+\\{', 'g'), item => { @@ -675,28 +681,48 @@ export function sourceReplace(source: string): string { return `${item} constructor(${COMPONENT_CONSTRUCTOR_ID}?, ${COMPONENT_CONSTRUCTOR_PARENT}?, ${COMPONENT_CONSTRUCTOR_PARAMS}?) {}`; }); - content = preprocessExtend(content); - content = preprocessNewExtend(content); + content = preprocessExtend(content, sourcePath, log); // process @system. content = processSystemApi(content); - return content; -} - -export function preprocessExtend(content: string): string { - const REG_EXTEND: RegExp = /(? { - collectExtend(item3, item4, item6); - return `${item1}${COMPONENT_EXTEND_DECORATOR} function${item2}__${item3}__${item4}${item5}${item6}${item7}${item3}`; - }); + return { + content: content, + log: log + } } -export function preprocessNewExtend(content: string): string { - const REG_EXTEND: RegExp = /(? { - collectExtend(item2, item5, item7); - return `${item1}${COMPONENT_EXTEND_DECORATOR}${item3}function${item4}__${item2}__${item5}${item6}${item7}${item8}${item2}`; - }); +export function preprocessExtend(content: string, sourcePath: string, log: LogInfo[]): string { + let pegCheckContent + let result + try { + result = parser.parse(content); + pegCheckContent = result.content; + for (let i=0; i Date: Tue, 12 Oct 2021 17:38:21 +0800 Subject: [PATCH 02/12] houhaoyu@huawei.com check extend rule correct or not Signed-off-by: houhaoyu Change-Id: Ic8e10a925224304cf907e7c09dd7eb87d42d276e --- compiler/build_peg.js | 38 +++++-- compiler/peg_parser/src/index.peg | 133 ++++++++++++----------- compiler/peg_parser/src/parse_extend.peg | 2 +- compiler/src/pre_process.ts | 10 +- compiler/src/validate_ui_syntax.ts | 20 ++-- 5 files changed, 110 insertions(+), 93 deletions(-) diff --git a/compiler/build_peg.js b/compiler/build_peg.js index 42183ebfa..b0db85f2b 100644 --- a/compiler/build_peg.js +++ b/compiler/build_peg.js @@ -1,24 +1,38 @@ +/* + * 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 util = require('util'); const child_process = require('child_process'); const exec = util.promisify(child_process.exec); const fs = require('fs'); const path = require('path'); + const readDir = fs.readdirSync('./peg_parser/src/'); const catalog = fs.readdirSync('./peg_parser/'); -if (catalog.indexOf('dist')>-1) { - exec('rm -rf peg_parser/dist'); +if (catalog.includes('dist')) { + exec('rm -rf peg_parser/dist'); } -exec('mkdir peg_parser/dist') +exec('mkdir peg_parser/dist'); -const pegTransJs = async function () { - if (readDir.length) { - for (let item of readDir) { - let name = path.basename(item, '.peg'); - await exec('pegjs -o peg_parser/dist/' + name + '.js peg_parser/src/' + item); - } +(async function pegTransJs () { + if (readDir.length) { + for (let item of readDir) { + let name = path.basename(item, '.peg'); + await exec('pegjs -o peg_parser/dist/' + name + '.js peg_parser/src/' + item); } -} - -pegTransJs(); \ No newline at end of file + } +})() \ No newline at end of file diff --git a/compiler/peg_parser/src/index.peg b/compiler/peg_parser/src/index.peg index c06d8ab4b..a7a31c89c 100644 --- a/compiler/peg_parser/src/index.peg +++ b/compiler/peg_parser/src/index.peg @@ -14,29 +14,31 @@ */ { - const parse_extend = require('./parse_extend.js') - let error_otherParsers = [] - let singleError = function(errMessage, line, column) { - this.errMessage = errMessage; - this.errPosition = { - line: line, - column: column - } - } - let collect_extend = { - component: [], - functionName: [], - parameters: [] - } + 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+ { - return { - content: blocks.join(''), - location: location(), - collect_extend: collect_extend, - error_otherParsers: error_otherParsers - } +start = blocks: block+ +{ + return { + content: blocks.join(''), + location: location(), + collect_extend: collect_extend, + error_otherParsers: error_otherParsers + }; } block = extend/(prefix extend?) @@ -44,37 +46,37 @@ block = extend/(prefix extend?) prefix = $((!prefix_extend .)+) extend = -whiteSpace1:whiteSpace prefix_extend:prefix_extend funcBody_extend:funcBody_extend -whiteSpace2: whiteSpace right:'}' whiteSpace3:whiteSpace + whiteSpace1:whiteSpace prefix_extend:prefix_extend funcBody_extend:funcBody_extend + whiteSpace2: whiteSpace right:'}' whiteSpace3:whiteSpace { - return whiteSpace1 + prefix_extend + funcBody_extend + whiteSpace2 + right + whiteSpace3 + 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:'{' + '@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 + 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:'{' + '@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 + 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_]+ @@ -88,33 +90,34 @@ function_name_tail = $ [a-zA-Z0-9_$]* parameters = $ ([^()]* item* [^()]*) //extract Extend internal attribute SyntaxError -funcBody_extend = body:($[^{}]* item* [^{}]*) { - try { - let functionBody = body.join('') - parse_extend.parse(functionBody) - return functionBody - } 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 +funcBody_extend = body:($[^{}]* item* [^{}]*) +{ + try { + let functionBody = body.join(''); + parse_extend.parse(functionBody); + return functionBody; + } 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) +item = $ (whiteSpace '{' [^{}]* item* [^{}]* '}' whiteSpace) -whiteSpace = $[ \t\r\n]* \ No newline at end of file +whiteSpace = $ [ \t\r\n]* \ No newline at end of file diff --git a/compiler/peg_parser/src/parse_extend.peg b/compiler/peg_parser/src/parse_extend.peg index 8dc9aae08..aa57c0461 100644 --- a/compiler/peg_parser/src/parse_extend.peg +++ b/compiler/peg_parser/src/parse_extend.peg @@ -15,7 +15,7 @@ start = extendAttributes:extendAttribute+ { - return {location: location()} + return {location: location()} } extendAttribute = whiteSpace '.' whiteSpace attribute item diff --git a/compiler/src/pre_process.ts b/compiler/src/pre_process.ts index 3dbfa1e4f..92d610a67 100644 --- a/compiler/src/pre_process.ts +++ b/compiler/src/pre_process.ts @@ -14,7 +14,7 @@ */ import { - afterExtendCheck, + replaceResult, sourceReplace, validateUISyntax, processSystemApi @@ -28,14 +28,14 @@ import { BUILD_ON } from './pre_define'; function preProcess(source: string): string { process.env.compiler = BUILD_ON; if (/\.ets$/.test(this.resourcePath)) { - const result: afterExtendCheck = sourceReplace(source, this.resourcePath) - const content: string = result.content + const result: replaceResult = sourceReplace(source, this.resourcePath) + const newContent: string = result.content const log: LogInfo[] = result.log - log.concat(validateUISyntax(source, content, this.resourcePath, this.resourceQuery)); + log.concat(validateUISyntax(source, newContent, this.resourcePath, this.resourceQuery)); if (log.length) { emitLogInfo(this, log); } - return content; + return newContent; } else { return processSystemApi(source); } diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 614f6797b..e25866ab0 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -15,7 +15,6 @@ import ts from 'typescript'; import path from 'path'; -const parser = require('../peg_parser/dist/index.js') import { INNER_COMPONENT_DECORATORS, @@ -60,6 +59,7 @@ import { addLog, hasDecorator } from './utils'; +const parser = require('../peg_parser/dist/index.js') export interface ComponentCollection { entryComponent: string; @@ -666,12 +666,12 @@ function collectionStates(decorator: string, name: string, states: Set, } } -export interface afterExtendCheck { +export interface replaceResult { content: string, log: LogInfo[] } -export function sourceReplace(source: string, sourcePath: string): afterExtendCheck { +export function sourceReplace(source: string, sourcePath: string): replaceResult { let content: string = source; const log: LogInfo[] = []; // replace struct->class @@ -692,13 +692,13 @@ export function sourceReplace(source: string, sourcePath: string): afterExtendCh } export function preprocessExtend(content: string, sourcePath: string, log: LogInfo[]): string { - let pegCheckContent - let result + let pegCheckContent; + let result; try { result = parser.parse(content); pegCheckContent = result.content; for (let i=0; i Date: Tue, 12 Oct 2021 18:15:45 +0800 Subject: [PATCH 03/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: I56a8b16c1abb6e92d6635e652ea717f676c26187 --- compiler/peg_parser/src/parse_extend.peg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/peg_parser/src/parse_extend.peg b/compiler/peg_parser/src/parse_extend.peg index aa57c0461..7382cc6c9 100644 --- a/compiler/peg_parser/src/parse_extend.peg +++ b/compiler/peg_parser/src/parse_extend.peg @@ -15,7 +15,7 @@ start = extendAttributes:extendAttribute+ { - return {location: location()} + return {location: location()}; } extendAttribute = whiteSpace '.' whiteSpace attribute item -- Gitee From 72c3e2d41a0d9c4fd2e1c92ec0435f3309c6301c Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Tue, 12 Oct 2021 20:10:01 +0800 Subject: [PATCH 04/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: I1b2c3eb0dc703e2f613ed53a9bbcd65d148c4663 --- compiler/peg_parser/src/parse_extend.peg | 5 ---- .../src/{index.peg => peg_parser.peg} | 7 ------ compiler/src/pre_process.ts | 8 +++---- compiler/src/validate_ui_syntax.ts | 24 +++++++++++-------- 4 files changed, 18 insertions(+), 26 deletions(-) rename compiler/peg_parser/src/{index.peg => peg_parser.peg} (99%) diff --git a/compiler/peg_parser/src/parse_extend.peg b/compiler/peg_parser/src/parse_extend.peg index 7382cc6c9..b10f1489c 100644 --- a/compiler/peg_parser/src/parse_extend.peg +++ b/compiler/peg_parser/src/parse_extend.peg @@ -19,13 +19,8 @@ start = extendAttributes:extendAttribute+ } extendAttribute = whiteSpace '.' whiteSpace attribute item - attribute = attr_head attr_tail - attr_head = $ [a-zA-Z_$]+ - attr_tail = $ [a-zA-Z0-9_$]* - item = whiteSpace '(' [^()]* item* [^()]* ')' whiteSpace - whiteSpace = [ \t\r\n]* \ No newline at end of file diff --git a/compiler/peg_parser/src/index.peg b/compiler/peg_parser/src/peg_parser.peg similarity index 99% rename from compiler/peg_parser/src/index.peg rename to compiler/peg_parser/src/peg_parser.peg index a7a31c89c..15d194baf 100644 --- a/compiler/peg_parser/src/index.peg +++ b/compiler/peg_parser/src/peg_parser.peg @@ -42,9 +42,7 @@ start = blocks: block+ } block = extend/(prefix extend?) - prefix = $((!prefix_extend .)+) - extend = whiteSpace1:whiteSpace prefix_extend:prefix_extend funcBody_extend:funcBody_extend whiteSpace2: whiteSpace right:'}' whiteSpace3:whiteSpace @@ -80,13 +78,9 @@ prefix_extend_another = } 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 @@ -119,5 +113,4 @@ funcBody_extend = body:($[^{}]* item* [^{}]*) } item = $ (whiteSpace '{' [^{}]* item* [^{}]* '}' whiteSpace) - whiteSpace = $ [ \t\r\n]* \ No newline at end of file diff --git a/compiler/src/pre_process.ts b/compiler/src/pre_process.ts index 92d610a67..a95b88b51 100644 --- a/compiler/src/pre_process.ts +++ b/compiler/src/pre_process.ts @@ -14,7 +14,7 @@ */ import { - replaceResult, + ReplaceResult, sourceReplace, validateUISyntax, processSystemApi @@ -28,9 +28,9 @@ import { BUILD_ON } from './pre_define'; function preProcess(source: string): string { process.env.compiler = BUILD_ON; if (/\.ets$/.test(this.resourcePath)) { - const result: replaceResult = sourceReplace(source, this.resourcePath) - const newContent: string = result.content - const log: LogInfo[] = result.log + const result: ReplaceResult = sourceReplace(source, this.resourcePath); + const newContent: string = result.content; + const log: LogInfo[] = result.log; log.concat(validateUISyntax(source, newContent, this.resourcePath, this.resourceQuery)); if (log.length) { emitLogInfo(this, log); diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index e25866ab0..c2e3e835d 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -59,7 +59,7 @@ import { addLog, hasDecorator } from './utils'; -const parser = require('../peg_parser/dist/index.js') +const parser = require('../peg_parser/dist/peg_parser.js'); export interface ComponentCollection { entryComponent: string; @@ -666,12 +666,12 @@ function collectionStates(decorator: string, name: string, states: Set, } } -export interface replaceResult { +export interface ReplaceResult { content: string, log: LogInfo[] } -export function sourceReplace(source: string, sourcePath: string): replaceResult { +export function sourceReplace(source: string, sourcePath: string): ReplaceResult { let content: string = source; const log: LogInfo[] = []; // replace struct->class @@ -692,27 +692,31 @@ export function sourceReplace(source: string, sourcePath: string): replaceResult } export function preprocessExtend(content: string, sourcePath: string, log: LogInfo[]): string { - let pegCheckContent; - let result; + let pegCheckContent: string; + let result: any; try { result = parser.parse(content); pegCheckContent = result.content; - for (let i=0; i Date: Tue, 12 Oct 2021 20:14:42 +0800 Subject: [PATCH 05/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: I6bee9d5b48a0aab0860e413b051a1041ee178cc4 --- compiler/build_peg.js | 2 +- compiler/peg_parser/src/parse_extend.peg | 2 +- compiler/peg_parser/src/peg_parser.peg | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/build_peg.js b/compiler/build_peg.js index b0db85f2b..f302671f2 100644 --- a/compiler/build_peg.js +++ b/compiler/build_peg.js @@ -35,4 +35,4 @@ exec('mkdir peg_parser/dist'); await exec('pegjs -o peg_parser/dist/' + name + '.js peg_parser/src/' + item); } } -})() \ No newline at end of file +})() diff --git a/compiler/peg_parser/src/parse_extend.peg b/compiler/peg_parser/src/parse_extend.peg index b10f1489c..0dcec46be 100644 --- a/compiler/peg_parser/src/parse_extend.peg +++ b/compiler/peg_parser/src/parse_extend.peg @@ -23,4 +23,4 @@ attribute = attr_head attr_tail attr_head = $ [a-zA-Z_$]+ attr_tail = $ [a-zA-Z0-9_$]* item = whiteSpace '(' [^()]* item* [^()]* ')' whiteSpace -whiteSpace = [ \t\r\n]* \ No newline at end of file +whiteSpace = [ \t\r\n]* diff --git a/compiler/peg_parser/src/peg_parser.peg b/compiler/peg_parser/src/peg_parser.peg index 15d194baf..c77c99afc 100644 --- a/compiler/peg_parser/src/peg_parser.peg +++ b/compiler/peg_parser/src/peg_parser.peg @@ -113,4 +113,4 @@ funcBody_extend = body:($[^{}]* item* [^{}]*) } item = $ (whiteSpace '{' [^{}]* item* [^{}]* '}' whiteSpace) -whiteSpace = $ [ \t\r\n]* \ No newline at end of file +whiteSpace = $ [ \t\r\n]* -- Gitee From 2c5e2134c694f8e26ace558132b456ec246abfa5 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Wed, 13 Oct 2021 09:51:45 +0800 Subject: [PATCH 06/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: Id40b255adce67fa387e5523cdb86fd84c77220fa --- compiler/build_peg.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/build_peg.js b/compiler/build_peg.js index f302671f2..a35441a5e 100644 --- a/compiler/build_peg.js +++ b/compiler/build_peg.js @@ -14,8 +14,8 @@ */ const util = require('util'); -const child_process = require('child_process'); -const exec = util.promisify(child_process.exec); +const childProcess = require('child_process'); +const exec = util.promisify(childProcess.exec); const fs = require('fs'); const path = require('path'); -- Gitee From 63e3592969d3bbb415a50740bc2a1520fc2e45cc Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 15 Oct 2021 09:27:21 +0800 Subject: [PATCH 07/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: I85b8d52735994822082b14544e9139b8b9598579 --- BUILD.gn | 19 +++++++++++++++++++ build_ets_loader_library.py | 14 +++++++++++--- compiler/build_peg.js | 34 ++++++++++++++++++++-------------- compiler/package.json | 3 ++- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index ea0dcd1a7..7fd00497b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -6,6 +6,8 @@ import("//foundation/ace/ace_engine/ace_config.gni") ets_loader_lib_dir = get_label_info(":build_ets_loader_library", "target_out_dir") + "/lib" +ets_loader_peg_dir = + get_label_info(":build_ets_loader_library", "target_out_dir") + "/peg_parser/dist" ets_loader_declarations_dir = get_label_info(":build_ets_loader_library", "target_out_dir") + "/declarations" ets_loader_component_config_file = @@ -17,6 +19,7 @@ action("build_ets_loader_library") { depfile = "$target_gen_dir/$target_name.d" outputs = [ ets_loader_lib_dir, + ets_loader_peg_dir, ets_loader_declarations_dir, ets_loader_component_config_file, ] @@ -27,12 +30,14 @@ action("build_ets_loader_library") { _babel_js = _ets_loader_dir + "/node_modules/@babel/cli/bin/babel.js" _babel_config_js = _ets_loader_dir + "/babel.config.js" _uglify_source_js = _ets_loader_dir + "/uglify-source.js" + _build_peg_js = _ets_loader_dir + "/build_peg.js" _build_declarations_file_js = _ets_loader_dir + "/build_declarations_file.js" inputs = [ _babel_config_js, _babel_js, _uglify_source_js, + _build_peg_js, _build_declarations_file_js, ] @@ -56,12 +61,18 @@ action("build_ets_loader_library") { rebase_path(_babel_js, root_build_dir), "--ets-loader-src-dir", rebase_path(_ets_loader_dir + "/src", root_build_dir), + "--ets-loader-peg-src-dir", + rebase_path(_ets_loader_dir + "/peg_parser/src", root_build_dir), "--babel-config-js", rebase_path(_babel_config_js, root_build_dir), "--uglify-source-js", rebase_path(_uglify_source_js, root_build_dir), + "--build-peg-js", + rebase_path(_build_peg_js, root_build_dir), "--output-dir", rebase_path(ets_loader_lib_dir, root_build_dir), + "--output-peg-dir", + rebase_path(ets_loader_peg_dir, root_build_dir), "--declarations-file-dir", rebase_path(_declarations_file_dir, root_build_dir), "--build-declarations-file-js", @@ -97,6 +108,14 @@ ohos_copy("ets_loader_library") { module_install_name = "" } +ohos_copy("ets_loader_peg") { + deps = [ ":build_ets_loader_library" ] + sources = [ ets_loader_peg_dir ] + outputs = [ target_out_dir + "/$target_name" ] + module_source_dir = target_out_dir + "/$target_name" + module_install_name = "" +} + ohos_copy("ets_loader_declaration") { deps = [ ":build_ets_loader_library" ] sources = [ ets_loader_declarations_dir ] diff --git a/build_ets_loader_library.py b/build_ets_loader_library.py index 462f7dbe5..ea1c0bc44 100755 --- a/build_ets_loader_library.py +++ b/build_ets_loader_library.py @@ -31,9 +31,12 @@ def parse_args(): parser.add_argument('--node', help='path to nodejs exetuable') parser.add_argument('--babel-js', help='path to babel.js') parser.add_argument('--ets-loader-src-dir', help='path to compiler/src') + parser.add_argument('--ets-loader-peg-src-dir', help='path to compiler/peg_parser/src') parser.add_argument('--babel-config-js', help='path babel.config.js') parser.add_argument('--uglify-source-js', help='path uglify-source.js') + parser.add_argument('--build-peg-js', help='path build_peg.js') parser.add_argument('--output-dir', help='path to output') + parser.add_argument('--output-peg-dir', help='path peg file to output') parser.add_argument('--declarations-file-dir', help='path declarations file') parser.add_argument('--build-declarations-file-js', @@ -47,8 +50,8 @@ def parse_args(): return options -def do_build(build_cmd, uglify_cmd, build_declarations_file_cmd): - for cmd in [build_cmd, uglify_cmd, build_declarations_file_cmd]: +def do_build(build_cmd, uglify_cmd, peg_cmd, build_declarations_file_cmd): + for cmd in [build_cmd, uglify_cmd, peg_cmd, build_declarations_file_cmd]: build_utils.check_output(cmd) @@ -62,10 +65,14 @@ def main(): build_cmd.extend(['--config-file', options.babel_config_js]) depfile_deps = [options.node, options.babel_js, options.babel_config_js] depfile_deps.extend(build_utils.get_all_files(options.ets_loader_src_dir)) + depfile_deps.extend(build_utils.get_all_files(options.ets_loader_peg_src_dir)) uglify_cmd = [options.node, options.uglify_source_js, options.output_dir] depfile_deps.append(options.uglify_source_js) + peg_cmd = [options.node, options.build_peg_js, options.output_peg_dir] + depfile_deps.append(options.build_peg_js) + build_declarations_file_cmd = [options.node, options.build_declarations_file_js, options.declarations_file_dir, @@ -74,12 +81,13 @@ def main(): depfile_deps.append(options.build_declarations_file_js) build_utils.call_and_write_depfile_if_stale( - lambda: do_build(build_cmd, uglify_cmd, build_declarations_file_cmd), + lambda: do_build(build_cmd, uglify_cmd, peg_cmd, build_declarations_file_cmd), options, depfile_deps=depfile_deps, input_paths=depfile_deps, output_paths=([ options.output_dir, + options.output_peg_dir, options.output_declarations_dir, options.output_component_config_file])) diff --git a/compiler/build_peg.js b/compiler/build_peg.js index a35441a5e..31741325e 100644 --- a/compiler/build_peg.js +++ b/compiler/build_peg.js @@ -19,20 +19,26 @@ const exec = util.promisify(childProcess.exec); const fs = require('fs'); const path = require('path'); -const readDir = fs.readdirSync('./peg_parser/src/'); -const catalog = fs.readdirSync('./peg_parser/'); +function generatePeg(inputFile) { + const readDirPath = path.resolve(__dirname, './peg_parser/src/'); + const readDirSubFiles = fs.readdirSync(readDirPath); + const catalogPath = path.resolve(inputFile, '..'); + const catalogSubFiles = fs.readdirSync(catalogPath) + + if (catalogSubFiles.includes('dist')) { + exec('rm -rf ' + catalogPath + '/dist/*.js'); + } else { + exec('mkdir ' + catalogPath + '/dist'); + } -if (catalog.includes('dist')) { - exec('rm -rf peg_parser/dist'); + (async function pegTransJs () { + if (readDirSubFiles.length) { + for (let item of readDirSubFiles) { + let name = path.basename(item, '.peg'); + await exec('pegjs -o ' + catalogPath + '/dist/' + name + '.js ' + readDirPath + '/' + item); + } + } + })() } -exec('mkdir peg_parser/dist'); - -(async function pegTransJs () { - if (readDir.length) { - for (let item of readDir) { - let name = path.basename(item, '.peg'); - await exec('pegjs -o peg_parser/dist/' + name + '.js peg_parser/src/' + item); - } - } -})() +generatePeg(process.argv[2]); diff --git a/compiler/package.json b/compiler/package.json index 509f94b28..6457c579c 100644 --- a/compiler/package.json +++ b/compiler/package.json @@ -10,11 +10,12 @@ ], "scripts": { "lint": "eslint --fix ./src --ext .ts", - "build": "node build_peg.js && npm run generateDeclarations && ./node_modules/.bin/babel ./src --out-dir lib --extensions .ts", + "build": "npm run generatePeg && npm run generateDeclarations && ./node_modules/.bin/babel ./src --out-dir lib --extensions .ts", "create": "node ./lib/create.js --env projectName", "compile": "webpack --config webpack.config.js --env buildMode=debug projectName", "test": "npm run build && mocha -r ts-node/register test/test.ts", "generateDeclarations": "node ./build_declarations_file.js ../../../interface/sdk-js/api/@internal/component/ets ./declarations ./lib", + "generatePeg": "node build_peg.js ./peg_parser/dist", "postinstall": "node npm-install.js" }, "devDependencies": { -- Gitee From 450cfcda06aa4632fb36a8cea22ac7c65169c0f4 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 15 Oct 2021 09:46:58 +0800 Subject: [PATCH 08/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: Iba7e01723c2dd892566a3be35cc37fc74a5e3499 --- BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/BUILD.gn b/BUILD.gn index 7366e0940..f0e318016 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -48,6 +48,7 @@ action("build_ets_loader_library") { _babel_js = _ace_config_dir + "/node_modules/@babel/cli/bin/babel.js" _babel_config_js = _ace_config_dir + "/babel.config.js" _uglify_source_js = _ace_config_dir + "/uglify-source.js" + _build_peg_js = _ace_config_dir + "/build_peg.js" _build_declarations_file_js = _ace_config_dir + "/build_declarations_file.js" inputs = [ -- Gitee From baea78f176d3ca2f9261cdacf5963a0cbc0a640c Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 15 Oct 2021 10:15:04 +0800 Subject: [PATCH 09/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: Iabd595b81a9cec9575410a7b67f88be6caf3f981 --- build_ets_loader_library.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/build_ets_loader_library.py b/build_ets_loader_library.py index 8dc63f4bd..49d289a5d 100755 --- a/build_ets_loader_library.py +++ b/build_ets_loader_library.py @@ -39,7 +39,8 @@ def parse_args(): parser.add_argument('--node', help='path to nodejs exetuable') parser.add_argument('--babel-js', help='path to babel.js') parser.add_argument('--ets-loader-src-dir', help='path to compiler/src') - parser.add_argument('--ets-loader-peg-src-dir', help='path to compiler/peg_parser/src') + parser.add_argument('--ets-loader-peg-src-dir', + help='path to compiler/peg_parser/src') parser.add_argument('--babel-config-js', help='path babel.config.js') parser.add_argument('--uglify-source-js', help='path uglify-source.js') parser.add_argument('--build-peg-js', help='path build_peg.js') @@ -73,7 +74,8 @@ def main(): build_cmd.extend(['--config-file', options.babel_config_js]) depfile_deps = [options.node, options.babel_js, options.babel_config_js] depfile_deps.extend(build_utils.get_all_files(options.ets_loader_src_dir)) - depfile_deps.extend(build_utils.get_all_files(options.ets_loader_peg_src_dir)) + depfile_deps.extend( + build_utils.get_all_files(options.ets_loader_peg_src_dir)) uglify_cmd = [options.node, options.uglify_source_js, options.output_dir] depfile_deps.append(options.uglify_source_js) @@ -89,7 +91,8 @@ def main(): depfile_deps.append(options.build_declarations_file_js) build_utils.call_and_write_depfile_if_stale( - lambda: do_build(build_cmd, uglify_cmd, peg_cmd, build_declarations_file_cmd), + lambda: do_build(build_cmd, uglify_cmd, + peg_cmd, build_declarations_file_cmd), options, depfile_deps=depfile_deps, input_paths=depfile_deps, -- Gitee From 7a1b576b4ccbf707e3149b5b3790fd64e0984772 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 15 Oct 2021 12:04:25 +0800 Subject: [PATCH 10/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: Iad26c018c61e5c3bea0e128051e8e9f74b132741 --- BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index f0e318016..f7df579e4 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -17,8 +17,8 @@ import("//foundation/ace/ace_engine/ace_config.gni") ets_loader_lib_dir = get_label_info(":build_ets_loader_library", "target_out_dir") + "/lib" -ets_loader_peg_dir = - get_label_info(":build_ets_loader_library", "target_out_dir") + "/peg_parser/dist" +ets_loader_peg_dir = get_label_info(":build_ets_loader_library", + "target_out_dir") + "/peg_parser/dist" ets_loader_declarations_dir = get_label_info(":build_ets_loader_library", "target_out_dir") + "/declarations" ets_loader_component_config_file = -- Gitee From 8f16d575d52649abb364a3e441f14bd7fa6c0362 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 15 Oct 2021 14:53:42 +0800 Subject: [PATCH 11/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: I5a093723aff87f4f21e1c7c6b478142967c6058a --- compiler/build_peg.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/build_peg.js b/compiler/build_peg.js index 31741325e..b1cb34d7f 100644 --- a/compiler/build_peg.js +++ b/compiler/build_peg.js @@ -20,10 +20,11 @@ const fs = require('fs'); const path = require('path'); function generatePeg(inputFile) { - const readDirPath = path.resolve(__dirname, './peg_parser/src/'); + const readDirPath = path.resolve(__dirname, './peg_parser/src'); const readDirSubFiles = fs.readdirSync(readDirPath); const catalogPath = path.resolve(inputFile, '..'); const catalogSubFiles = fs.readdirSync(catalogPath) + const pegjs = path.resolve(__dirname, './node_modules/pegjs/bin/pegjs'); if (catalogSubFiles.includes('dist')) { exec('rm -rf ' + catalogPath + '/dist/*.js'); @@ -31,11 +32,13 @@ function generatePeg(inputFile) { exec('mkdir ' + catalogPath + '/dist'); } - (async function pegTransJs () { + ;(async function pegTransJs () { if (readDirSubFiles.length) { for (let item of readDirSubFiles) { let name = path.basename(item, '.peg'); - await exec('pegjs -o ' + catalogPath + '/dist/' + name + '.js ' + readDirPath + '/' + item); + if (name){ + await exec(pegjs + ' -o ' + catalogPath + '/dist/' + name + '.js ' + readDirPath + '/' + item); + } } } })() -- Gitee From 34557540a6f8678ee3bf20e51f8f3fbd8a800e00 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 15 Oct 2021 16:22:32 +0800 Subject: [PATCH 12/12] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: I0b07be5a73eb7e153a57fa5c0d6f678eaaf20794 --- BUILD.gn | 26 +++++++++---------- build_ets_loader_library.py | 25 ++++++++++-------- compiler/{build_peg.js => build_parser.js} | 13 +++++----- compiler/package.json | 4 +-- compiler/src/validate_ui_syntax.ts | 10 +++---- .../src/parse_extend.peg | 0 .../src/syntax_parser.peg} | 0 7 files changed, 41 insertions(+), 37 deletions(-) rename compiler/{build_peg.js => build_parser.js} (74%) rename compiler/{peg_parser => syntax_parser}/src/parse_extend.peg (100%) rename compiler/{peg_parser/src/peg_parser.peg => syntax_parser/src/syntax_parser.peg} (100%) diff --git a/BUILD.gn b/BUILD.gn index f7df579e4..0535ce60d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -17,8 +17,8 @@ import("//foundation/ace/ace_engine/ace_config.gni") ets_loader_lib_dir = get_label_info(":build_ets_loader_library", "target_out_dir") + "/lib" -ets_loader_peg_dir = get_label_info(":build_ets_loader_library", - "target_out_dir") + "/peg_parser/dist" +ets_loader_syntax_dir = get_label_info(":build_ets_loader_library", + "target_out_dir") + "/syntax_parser/dist" ets_loader_declarations_dir = get_label_info(":build_ets_loader_library", "target_out_dir") + "/declarations" ets_loader_component_config_file = @@ -32,7 +32,7 @@ action("build_ets_loader_library") { depfile = "$target_gen_dir/$target_name.d" outputs = [ ets_loader_lib_dir, - ets_loader_peg_dir, + ets_loader_syntax_dir, ets_loader_declarations_dir, ets_loader_component_config_file, ] @@ -48,14 +48,14 @@ action("build_ets_loader_library") { _babel_js = _ace_config_dir + "/node_modules/@babel/cli/bin/babel.js" _babel_config_js = _ace_config_dir + "/babel.config.js" _uglify_source_js = _ace_config_dir + "/uglify-source.js" - _build_peg_js = _ace_config_dir + "/build_peg.js" + _build_parser_js = _ace_config_dir + "/build_parser.js" _build_declarations_file_js = _ace_config_dir + "/build_declarations_file.js" inputs = [ _babel_config_js, _babel_js, _uglify_source_js, - _build_peg_js, + _build_parser_js, _build_declarations_file_js, ] @@ -87,18 +87,18 @@ action("build_ets_loader_library") { rebase_path(_babel_js, root_build_dir), "--ets-loader-src-dir", rebase_path(_ets_loader_dir + "/src", root_build_dir), - "--ets-loader-peg-src-dir", - rebase_path(_ets_loader_dir + "/peg_parser/src", root_build_dir), + "--ets-loader-syntax-src-dir", + rebase_path(_ets_loader_dir + "/syntax_parser/src", root_build_dir), "--babel-config-js", rebase_path(_babel_config_js, root_build_dir), "--uglify-source-js", rebase_path(_uglify_source_js, root_build_dir), - "--build-peg-js", - rebase_path(_build_peg_js, root_build_dir), + "--build-parser-js", + rebase_path(_build_parser_js, root_build_dir), "--output-dir", rebase_path(ets_loader_lib_dir, root_build_dir), - "--output-peg-dir", - rebase_path(ets_loader_peg_dir, root_build_dir), + "--output-syntax-dir", + rebase_path(ets_loader_syntax_dir, root_build_dir), "--declarations-file-dir", rebase_path(_declarations_file_dir, root_build_dir), "--build-declarations-file-js", @@ -154,9 +154,9 @@ ohos_copy("ets_loader_library") { module_install_name = "" } -ohos_copy("ets_loader_peg") { +ohos_copy("ets_loader_syntax") { deps = [ ":build_ets_loader_library" ] - sources = [ ets_loader_peg_dir ] + sources = [ ets_loader_syntax_dir ] outputs = [ target_out_dir + "/$target_name" ] module_source_dir = target_out_dir + "/$target_name" module_install_name = "" diff --git a/build_ets_loader_library.py b/build_ets_loader_library.py index 49d289a5d..4511664c5 100755 --- a/build_ets_loader_library.py +++ b/build_ets_loader_library.py @@ -39,13 +39,14 @@ def parse_args(): parser.add_argument('--node', help='path to nodejs exetuable') parser.add_argument('--babel-js', help='path to babel.js') parser.add_argument('--ets-loader-src-dir', help='path to compiler/src') - parser.add_argument('--ets-loader-peg-src-dir', - help='path to compiler/peg_parser/src') + parser.add_argument('--ets-loader-syntax-src-dir', + help='path to compiler/syntax_parser/src') parser.add_argument('--babel-config-js', help='path babel.config.js') parser.add_argument('--uglify-source-js', help='path uglify-source.js') - parser.add_argument('--build-peg-js', help='path build_peg.js') + parser.add_argument('--build-parser-js', help='path build_parser.js') parser.add_argument('--output-dir', help='path to output') - parser.add_argument('--output-peg-dir', help='path peg file to output') + parser.add_argument('--output-syntax-dir', + help='path syntax file to output') parser.add_argument('--declarations-file-dir', help='path declarations file') parser.add_argument('--build-declarations-file-js', @@ -59,8 +60,9 @@ def parse_args(): return options -def do_build(build_cmd, uglify_cmd, peg_cmd, build_declarations_file_cmd): - for cmd in [build_cmd, uglify_cmd, peg_cmd, build_declarations_file_cmd]: +def do_build(build_cmd, uglify_cmd, syntax_cmd, build_declarations_file_cmd): + for cmd in [build_cmd, uglify_cmd, syntax_cmd, + build_declarations_file_cmd]: build_utils.check_output(cmd) @@ -75,13 +77,14 @@ def main(): depfile_deps = [options.node, options.babel_js, options.babel_config_js] depfile_deps.extend(build_utils.get_all_files(options.ets_loader_src_dir)) depfile_deps.extend( - build_utils.get_all_files(options.ets_loader_peg_src_dir)) + build_utils.get_all_files(options.ets_loader_syntax_src_dir)) uglify_cmd = [options.node, options.uglify_source_js, options.output_dir] depfile_deps.append(options.uglify_source_js) - peg_cmd = [options.node, options.build_peg_js, options.output_peg_dir] - depfile_deps.append(options.build_peg_js) + syntax_cmd = [options.node, options.build_parser_js, + options.output_syntax_dir] + depfile_deps.append(options.build_parser_js) build_declarations_file_cmd = [options.node, options.build_declarations_file_js, @@ -92,13 +95,13 @@ def main(): build_utils.call_and_write_depfile_if_stale( lambda: do_build(build_cmd, uglify_cmd, - peg_cmd, build_declarations_file_cmd), + syntax_cmd, build_declarations_file_cmd), options, depfile_deps=depfile_deps, input_paths=depfile_deps, output_paths=([ options.output_dir, - options.output_peg_dir, + options.output_syntax_dir, options.output_declarations_dir, options.output_component_config_file])) diff --git a/compiler/build_peg.js b/compiler/build_parser.js similarity index 74% rename from compiler/build_peg.js rename to compiler/build_parser.js index b1cb34d7f..03f79ad7d 100644 --- a/compiler/build_peg.js +++ b/compiler/build_parser.js @@ -19,12 +19,12 @@ const exec = util.promisify(childProcess.exec); const fs = require('fs'); const path = require('path'); -function generatePeg(inputFile) { - const readDirPath = path.resolve(__dirname, './peg_parser/src'); +function generateSyntaxParser(inputFile, nodePath) { + const readDirPath = path.resolve(__dirname, './syntax_parser/src'); const readDirSubFiles = fs.readdirSync(readDirPath); const catalogPath = path.resolve(inputFile, '..'); const catalogSubFiles = fs.readdirSync(catalogPath) - const pegjs = path.resolve(__dirname, './node_modules/pegjs/bin/pegjs'); + const parserPath = path.resolve(__dirname, './node_modules/pegjs/bin/pegjs'); if (catalogSubFiles.includes('dist')) { exec('rm -rf ' + catalogPath + '/dist/*.js'); @@ -32,16 +32,17 @@ function generatePeg(inputFile) { exec('mkdir ' + catalogPath + '/dist'); } - ;(async function pegTransJs () { + ;(async function transJs () { if (readDirSubFiles.length) { for (let item of readDirSubFiles) { let name = path.basename(item, '.peg'); if (name){ - await exec(pegjs + ' -o ' + catalogPath + '/dist/' + name + '.js ' + readDirPath + '/' + item); + await exec(nodePath + ' ' + parserPath + ' -o ' + + catalogPath + '/dist/' + name + '.js ' + readDirPath + '/' + item); } } } })() } -generatePeg(process.argv[2]); +generateSyntaxParser(process.argv[2], process.argv[0]); diff --git a/compiler/package.json b/compiler/package.json index b23b9bb38..fe0cde331 100644 --- a/compiler/package.json +++ b/compiler/package.json @@ -10,12 +10,12 @@ ], "scripts": { "lint": "eslint --fix ./src --ext .ts", - "build": "npm run generatePeg && npm run generateDeclarations && ./node_modules/.bin/babel ./src --out-dir lib --extensions .ts", + "build": "npm run generateSyntaxParser && npm run generateDeclarations && ./node_modules/.bin/babel ./src --out-dir lib --extensions .ts", "create": "node ./lib/create.js --env projectName", "compile": "webpack --config webpack.config.js --env buildMode=debug projectName", "test": "npm run build && mocha -r ts-node/register test/test.ts", "generateDeclarations": "node ./build_declarations_file.js ../../../interface/sdk-js/api/@internal/component/ets ./declarations ./lib", - "generatePeg": "node build_peg.js ./peg_parser/dist", + "generateSyntaxParser": "node build_parser.js ./syntax_parser/dist", "postinstall": "node npm-install.js" }, "devDependencies": { diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index c2e3e835d..00c1ada8b 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -59,7 +59,7 @@ import { addLog, hasDecorator } from './utils'; -const parser = require('../peg_parser/dist/peg_parser.js'); +const parser = require('../syntax_parser/dist/syntax_parser.js'); export interface ComponentCollection { entryComponent: string; @@ -692,11 +692,11 @@ export function sourceReplace(source: string, sourcePath: string): ReplaceResult } export function preprocessExtend(content: string, sourcePath: string, log: LogInfo[]): string { - let pegCheckContent: string; + let syntaxCheckContent: string; let result: any; try { result = parser.parse(content); - pegCheckContent = result.content; + syntaxCheckContent = result.content; for (let i = 0; i < result.collect_extend.component.length; i++) { collectExtend( result.collect_extend.component[i], @@ -713,7 +713,7 @@ export function preprocessExtend(content: string, sourcePath: string, log: LogIn column: err.location.start.column, fileName: sourcePath }); - pegCheckContent = content; + syntaxCheckContent = content; } if (result.error_otherParsers) { for(let i = 0; i < result.error_otherParsers.length; i++) { @@ -726,7 +726,7 @@ export function preprocessExtend(content: string, sourcePath: string, log: LogIn }); } } - return pegCheckContent; + return syntaxCheckContent; } function collectExtend(component: string, attribute: string, parameter: string): void { diff --git a/compiler/peg_parser/src/parse_extend.peg b/compiler/syntax_parser/src/parse_extend.peg similarity index 100% rename from compiler/peg_parser/src/parse_extend.peg rename to compiler/syntax_parser/src/parse_extend.peg diff --git a/compiler/peg_parser/src/peg_parser.peg b/compiler/syntax_parser/src/syntax_parser.peg similarity index 100% rename from compiler/peg_parser/src/peg_parser.peg rename to compiler/syntax_parser/src/syntax_parser.peg -- Gitee