Skip to content

Commit

Permalink
add annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
peze committed Dec 28, 2023
1 parent 6d27057 commit ff7e5b9
Show file tree
Hide file tree
Showing 14 changed files with 1,038 additions and 25 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const util = require('./lib/util');
const Tag = require('./lib/tag');
const builtin = require('./lib/builtin');
const comment = require('./lib/comment');
const note = require('./lib/note');
const pkg = require('./package.json');

function parse(source, filePath) {
Expand All @@ -21,5 +22,6 @@ module.exports = {
util,
builtin,
comment,
note,
getChecker
};
20 changes: 14 additions & 6 deletions lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,19 @@ class Lexer extends BaseLexer {
}

if (this.peek === '@') {
return this.parseVID();
const vid = this.parseVID();

if(this.peek === '('){
return new WordToken(Tag.NOTE, vid, {
start,
end: this.loc()
});
}

return new WordToken(Tag.VID, vid, {
start,
end: this.loc()
});
}

if (this.peek === '&') {
Expand Down Expand Up @@ -520,7 +532,6 @@ class Lexer extends BaseLexer {
}

parseVID() {
let start = this.loc();
let str = '@';
this.getch();

Expand All @@ -535,10 +546,7 @@ class Lexer extends BaseLexer {
isDecimalDigit(this.peek) ||
this.peek === '_');

return new WordToken(Tag.VID, str, {
start,
end: this.loc()
});
return str;
}

}
Expand Down
8 changes: 8 additions & 0 deletions lib/note.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const comment = require('./comment');


module.exports = {
getNotes: comment.getBetweenComments
};
49 changes: 46 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ class Parser extends BaseParser {
constructor(lexer) {
super(lexer);
this.comments = new Map();
this.notes = new Map();
this.index = 0;
}

move() {
move() {
do {
this.look = this.lexer.scan();
this.look.index = ++this.index;
if (this.look.tag === Tag.COMMENT) {
this.comments.set(this.look.index, this.look);
}
if (this.look.tag === Tag.NOTE) {
this.notes.set(this.look.index, this.note());
}
} while (this.look.tag === Tag.COMMENT);
}

Expand Down Expand Up @@ -55,6 +59,30 @@ class Parser extends BaseParser {
return this.module();
}

note(){
const begin = this.getIndex();
const note = this.look;
this.match(Tag.NOTE);
this.match('(');
const arg = this.expr();
this.match(')');
let end = this.getIndex();
if (this.is(';')) {
end = this.getIndex();
this.move();
}
return {
type: 'note',
note,
arg,
loc: {
start: note.loc.start,
end: this.lexer.loc()
},
tokenRange: [begin, end]
};
}

extends() {
const begin = this.getIndex();
this.match(Tag.EXTENDS);
Expand Down Expand Up @@ -127,6 +155,7 @@ class Parser extends BaseParser {
type: 'module',
moduleBody: this.moduleBody(),
comments: this.comments,
notes: this.notes,
};
}

Expand Down Expand Up @@ -1106,11 +1135,25 @@ class Parser extends BaseParser {
objectField() {
// objectField = objectFieldName "=" expr
const begin = this.getIndex();
if(this.is(Tag.STRING)) {
const fieldName = this.look;
this.move();
this.match('=');
const expr = this.expr();
const end = this.getIndex();
return {
type: 'objectField',
fieldName: fieldName,
expr: expr,
tokenRange: [begin, end]
};
}

if (this.isID()) {
var fieldName = this.look;
const fieldName = this.look;
this.move();
this.match('=');
var expr = this.expr();
const expr = this.expr();
const end = this.getIndex();
return {
type: 'objectField',
Expand Down
7 changes: 4 additions & 3 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,6 @@ class TypeChecker {
isAsync: true,
local: local
};
if (ast.runtimeBody) {
this.visitObject(ast.runtimeBody, env);
}
env.local.set('__request', _model('$Request'));
this.visitAPIBody(ast.apiBody, env);

Expand All @@ -1103,6 +1100,10 @@ class TypeChecker {
this.error(`no return statement`, ast.apiName);
}
}

if (ast.runtimeBody) {
this.visitObject(ast.runtimeBody, env);
}
}

visitParams(ast, env) {
Expand Down
1 change: 1 addition & 0 deletions lib/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ exports.Tag = Object.freeze({
GTE: 40, // >=
LT: 41, // <
LTE: 42, // <=
NOTE: 43, // @ID()
});

var TagTip = {};
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/notes/Darafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"libraries": {
"OSS": "./oss.dara"
}
}
83 changes: 83 additions & 0 deletions test/fixtures/notes/main.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import OSS;
import './util.dara' Util;


@input('call');
model CallRequest extends OSS.File {
size: number,
}

@output('call');
model CallResponse {
size: number,
}

init();

@error([Util.MainFileError, Util.ExtendFileError, Util.ExtendSubFileError])
@returnMode({
aliasName = "execTaskAsync",
async = true,
callback = callback,
interval= 3,
times = 10,
input = data.size,
})
static function call(data: CallRequest): CallResponse {
@go('fmt.printv("%v", data)');
if(data.size > 100) {
throw new Util.MainFileError{
name = 'name',
size = 100,
};
} else if (data.size == 100){
throw new Util.ExtendFileError{
code = 'name',
size = 100,
};
} else if (data.size == 50){
throw new Util.ExtendSubFileError{
name = 'name',
size = 100,
};
}
return new CallResponse {
size = data.size,
};
}



@paginated({
aliasName = "getUsersPaginated",
policy = "token",
maxItemsDefault = 20,
totalCount = 100,
inputToken = req.nextToken,
outputToken = resp.nextToken,
maxItems = req.maxResults,
maxItemsDefault = 20,
pageTruncated = resp.truncated,
totalCount = 100,
items = resp.replicaPairs,
})
async function callback(size: number) throws: void{
var req = {
nextToken = '123',
maxResults = 100,
};

var resp = {
nextToken = '321',
truncated = false,
replicaPairs = 50
};
return;
}

@deprecated({
sub = callback,
deprecatedTime = "2022-03-03",
warning = "this is deprecated",
})
async function test(size: number) : void;
15 changes: 15 additions & 0 deletions test/fixtures/notes/oss.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

model File = {
name: string,
};

exception FileError = {
code: string,
}

model SubFile = {
file: {
name: string,
}
}

21 changes: 21 additions & 0 deletions test/fixtures/notes/util.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import OSS

model mainFile extends OSS.File {
size: number,
}

exception MainFileError extends OSS.File {
size: number,
}

exception ExtendFileError extends OSS.FileError {
size: number,
}

exception ExtendSubFileError extends OSS.SubFile.file {
size: number,
}

model ExtendSubFile extends OSS.SubFile.file {
size: number,
}
42 changes: 40 additions & 2 deletions test/lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ describe('lexer', function () {
]);
});

it('should ok with virtual method', function () {
it('should ok with note', function () {
expect(lex('@prop()', '__filename')).to.be.eql([
{ tag: 3, lexeme: '@prop',
{ tag: 43, lexeme: '@prop',
'loc': loc(1, 1, 1, 6)
},
{ tag: '(',
Expand All @@ -307,6 +307,44 @@ describe('lexer', function () {
'loc': loc(1, 7, 1, 7) },
{ tag: undefined, loc: loc(1, 8, 1, 8) }
]);

expect(lex('@prop("test")', '__filename')).to.be.eql([
{ tag: 43, lexeme: '@prop',
'loc': loc(1, 1, 1, 6)
},
{ tag: '(',
'loc': loc(1, 6, 1, 6) },
{ tag: 1,
string: 'test',
'loc': loc(1, 8, 1, 12) },
{ tag: ')',
'loc': loc(1, 13, 1, 13) },
{ tag: undefined, loc: loc(1, 14, 1, 14) }
]);

console.log('%j', lex('@prop({ key = "value" })', '__filename'));
expect(lex('@prop({ key = "value" })', '__filename')).to.be.eql([
{ tag: 43, lexeme: '@prop',
'loc': loc(1, 1, 1, 6)
},
{ tag: '(',
'loc': loc(1, 6, 1, 6) },
{ tag: '{',
'loc': loc(1, 7, 1, 7) },
{ tag: 2,
lexeme: 'key',
'loc': loc(1, 9, 1, 12)},
{ tag: '=',
'loc': loc(1, 13, 1, 13)},
{ tag: 1,
string: 'value',
'loc': loc(1, 16, 1, 21)},
{ tag: '}',
'loc': loc(1, 23, 1, 23) },
{ tag: ')',
'loc': loc(1, 24, 1, 24) },
{ tag: undefined, loc: loc(1, 25, 1, 25) }
]);
});

it('should ok with template', function () {
Expand Down
Loading

0 comments on commit ff7e5b9

Please sign in to comment.