This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shahar Soel
committed
Sep 8, 2017
1 parent
0c27008
commit 6df5348
Showing
3 changed files
with
188 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const JDLParser = require('./parser').JDLParser; | ||
const _ = require('lodash'); | ||
|
||
|
||
function buildAst(cst) { | ||
// eslint-disable-next-line no-use-before-define | ||
const astBuilderVisitor = new JDLAstBuilderVisitor(); | ||
const ast = astBuilderVisitor.visit(cst); | ||
return ast; | ||
} | ||
|
||
const BaseJDLCSTVisitor = new JDLParser().getBaseCstVisitorConstructor(); | ||
|
||
class JDLAstBuilderVisitor extends BaseJDLCSTVisitor { | ||
constructor() { | ||
super(); | ||
this.validateVisitor(); | ||
} | ||
|
||
prog(ctx) { | ||
return { | ||
entities: _.map(ctx.entityDecl, elm => this.visit(elm)), | ||
constants: _.map(ctx.constantDecl, elm => this.visit(elm)) | ||
}; | ||
} | ||
|
||
constantDecl(ctx) { | ||
return { | ||
name: ctx.NAME[0].image, | ||
value: parseInt(ctx.VALUE[0], 10) | ||
}; | ||
} | ||
|
||
entityDecl(ctx) { | ||
return { | ||
name: ctx.NAME[0].image, | ||
// ctx.entityTableNameDecl is optional which means | ||
// either an empty array or an array of a single element | ||
// the "this.visit" API will handle this transparently and return | ||
// undefined in the case of empty array. | ||
tableName: this.visit(ctx.entityTableNameDecl), | ||
fields: this.visit(ctx.entityBody) | ||
}; | ||
} | ||
|
||
entityTableNameDecl(ctx) { | ||
return ctx.NAME[0].image; | ||
} | ||
|
||
entityBody(ctx) { | ||
return _.map(ctx.fieldDec, elm => this.visit(elm)); | ||
} | ||
|
||
fieldDec(ctx) { | ||
return { | ||
name: ctx.NAME[0].image, | ||
// ctx.type is an array with a single item. | ||
// in that case: | ||
// this.visit(ctx.type) is equivalent to this.visit(ctx.type[0]) | ||
type: this.visit(ctx.type), | ||
validations: _.map(ctx.validation, elm => this.visit(elm)) | ||
}; | ||
} | ||
|
||
type(ctx) { | ||
return ctx.NAME[0].image; | ||
} | ||
|
||
validation(ctx) { | ||
// only one of these alternatives can exist at the same time. | ||
if (!_.isEmpty(ctx.REQUIRED)) { | ||
return { | ||
validationType: 'required' | ||
}; | ||
} else if (!_.isEmpty(ctx.minMaxValidation)) { | ||
return this.visit(ctx.minMaxValidation); | ||
} | ||
return this.visit(ctx.pattern); | ||
} | ||
|
||
minMaxValidation(ctx) { | ||
return { | ||
validationType: ctx.MIN_MAX_KEYWORD.image, | ||
limit: _.isEmpty(ctx.NAME) ? | ||
parseInt(ctx.INTEGER[0], 10) : | ||
ctx.NAME[0].image | ||
}; | ||
} | ||
|
||
pattern(ctx) { | ||
return { | ||
validationType: 'pattern', | ||
pattern: ctx.REGEX[0].image | ||
}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
buildAst | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* eslint-disable key-spacing, no-unused-expressions */ | ||
const expect = require('chai').expect; | ||
const parse = require('../../../lib/dsl/poc/api').parse; | ||
const buildAst = require('../../../lib/dsl/poc/ast_builder').buildAst; | ||
|
||
|
||
describe('JDL AST Builder poc', () => { | ||
it('can build an ast for a simple entity', () => { | ||
const input = 'entity Person { name string }'; | ||
const parseResult = parse(input); | ||
expect(parseResult.lexErrors).to.be.empty; | ||
expect(parseResult.parseErrors).to.be.empty; | ||
|
||
const ast = buildAst(parseResult.cst); | ||
expect(ast).to.deep.equal({ | ||
constants: [], | ||
entities: [ | ||
{ | ||
fields: [ | ||
{ | ||
name: 'name', | ||
type: 'string', | ||
validations: [] | ||
} | ||
], | ||
name: 'Person', | ||
tableName: undefined | ||
} | ||
] | ||
}); | ||
}); | ||
|
||
it('can build an ast for a complex JDL', () => { | ||
const input = ` | ||
entity Person | ||
{ | ||
name string, | ||
age number max(max_age) | ||
} | ||
max_age = 120 | ||
entity Job | ||
{ | ||
address string required | ||
} | ||
`; | ||
const parseResult = parse(input); | ||
expect(parseResult.lexErrors).to.be.empty; | ||
expect(parseResult.parseErrors).to.be.empty; | ||
|
||
const ast = buildAst(parseResult.cst); | ||
expect(ast).to.deep.equal({ | ||
constants: [], | ||
entities: [ | ||
{ | ||
fields: [ | ||
{ | ||
name: 'name', | ||
type: 'string', | ||
validations: [] | ||
} | ||
], | ||
name: 'Person', | ||
tableName: undefined | ||
} | ||
] | ||
}); | ||
}); | ||
}); |