Skip to content

Commit

Permalink
Initial implementation of a new parser (#209)
Browse files Browse the repository at this point in the history
This patch includes the following things:
  - Implement reading WebIDL files as IDL fragments in parallel.
  - Introduce a new parser to process individual IDL fragment.
    In this patch, it just creates AST(Abstract Syntax Tree) for now
    by using WebIDL2 parser.
  - Add `base/` and `new_parsaer/` to lint target list.

ISSUE=#207
  • Loading branch information
romandev committed Nov 26, 2017
1 parent d47d88f commit 9c747a9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
15 changes: 15 additions & 0 deletions docs/webidl_compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ internally.
### WebIDL compiler
The WebIDL compiler(or often we called bindings generator) transcompiles WebIDL
to Native codes such as C++ codes.

## Reading/Processing IDL Files in parallel
When reading/processing a single IDL file, it should be each independent task.
Then, we can extend our compiler easily to support multi-threading or
distributed compiling in the future.

## Parsing WebIDL
The parsing step is corresponding to the front-end part of compiler. The goal in
this step, is that creating IDL definition objects as intermediated results.

### Creating AST(Abstract Syntax Tree)
To parse WebIDL(exactly,
[IDL Fragment](https://heycam.github.io/webidl/#dfn-idl-fragment)),
we just use [WebIDL2 parser](https://github.com/w3c/webidl2.js). It provides a
trivial API called parse(). If we use the API, the WebIDL2 parser returns a AST.
23 changes: 17 additions & 6 deletions generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@
* limitations under the License.
*/

import * as assert from 'assert';
import * as file from 'generator/base/file';
import {Parser} from 'generator/new_parser/parser';

export async function run(idlFiles: string[]): Promise<number> {
assert(idlFiles.indexOf('test/test_interface.idl') >= 0);
assert(idlFiles.indexOf('examples/calculator.idl') >= 0);
assert(idlFiles.indexOf('examples/electron/native/electron_native.idl') >= 0);
async function processIDL(idlFilePath: string): Promise<void> {
const idlFragment: string = await file.read(idlFilePath);
await Parser.parse(idlFragment);
}

export async function run(idlFilePaths: string[]): Promise<number> {
const readIDLFileTasks: Promise<void>[] = [];
idlFilePaths.forEach((idlFilePath) => {
readIDLFileTasks.push(processIDL(idlFilePath));
});

// Not implemented yet.
try {
await Promise.all(readIDLFileTasks);
} catch (e) {
return e;
}

return 0;
}
29 changes: 29 additions & 0 deletions generator/new_parser/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2017 The Bacardi Authors.
*
* 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.
*/

import * as webidl from 'webidl2';

/**
* WebIDL Parser
*/
export class Parser {
public static async parse(idlFragment: string):
Promise<void> {
const astData: {}[] = webidl.parse(idlFragment);

// Not implemented yet.
}
}
8 changes: 6 additions & 2 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ gulp.task('lint_ts', () => {

return gulp.src([
'gulpfile.ts',
'generator/index.ts'
'generator/index.ts',
'generator/base/**/*.ts',
'generator/new_parser/**/*.ts'
])
.pipe(gulpTslint({
formatter: 'codeFrame',
Expand All @@ -129,7 +131,9 @@ gulp.task('lint_ts:fix', () => {

return gulp.src([
'gulpfile.ts',
'generator/index.ts'
'generator/index.ts',
'generator/base/**/*.ts',
'generator/new_parser/**/*.ts'
])
.pipe(gulpTslint({
formatter: 'codeFrame',
Expand Down
7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
"extends": "tslint-microsoft-contrib",
"rules": {
"export-name": false,
"function-name": false,
"indent": [true, 2],
"match-default-export-name": false,
"max-line-length": [true, 80],
"no-stateless-class": false,
"no-suspicious-comment": false,
"no-unsafe-any": false,
"strict-boolean-expressions": [
true,
"allow-null-union",
"allow-undefined-union"
],
"typedef": [
true,
"call-signature",
Expand Down

0 comments on commit 9c747a9

Please sign in to comment.