Skip to content

Commit

Permalink
fix the exprItem end the parse
Browse files Browse the repository at this point in the history
  • Loading branch information
peze committed Jan 14, 2025
1 parent f342d07 commit 99b0905
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 66 deletions.
137 changes: 75 additions & 62 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ class Parser extends BaseParser {
// array = "[" [ arrayItems ] "]"
// arrayItems = expr { "," expr }
const begin = this.getIndex();
var start = this.lexer.loc();
this.match('[');
var items = [];
if (this.look.tag === ']') {
Expand Down Expand Up @@ -1092,6 +1093,10 @@ class Parser extends BaseParser {
return {
type: 'array',
items: items,
loc: {
start: start,
end: this.lexer.loc()
},
tokenRange: [begin, end]
};
}
Expand Down Expand Up @@ -1421,83 +1426,87 @@ class Parser extends BaseParser {

// id.x = xxx, id.x(), id.x
if (this.look.tag === '.') {
const propertyPath = [];
while (this.look.tag === '.') {
this.move();
var prop = this.look;
this.matchID();
propertyPath.push(prop);
}

// id.x()
if (this.look.tag === '(') {
this.move();
const args = this.args();
this.match(')');
// Module.staticCall() or module.instanceCall()
return {
type: 'call',
left: {
type: 'static_or_instance_call',
id: id,
propertyPath: propertyPath
},
args: args,
loc: {
start: id.loc.start,
end: this.lexer.loc()
},
};
}
return this.properties(id);
}

if (this.look.tag === '[') {
this.move();
let accessKey = this.expr();
this.match(']');
// @id.x[]
return this.mapAccess({
type: 'map_access',
id: id,
propertyPath: propertyPath,
accessKey: accessKey,
loc: {
start: id.loc.start,
end: this.lexer.loc()
}
});
}
return {
type: 'variable',
id: id,
loc: id.loc
};
}

// id.x = xxx
if (this.look.tag === '=') {
this.move();
let expr = this.expr();
return {
type: 'assign',
left: {
type: 'property',
id,
propertyPath
},
expr: expr
};
}
properties(id){
const propertyPath = [];
while (this.look.tag === '.') {
this.move();
var prop = this.look;
this.matchID();
propertyPath.push(prop);
}

// id.x
// id.x()
if (this.look.tag === '(') {
this.move();
const args = this.args();
this.match(')');
// Module.staticCall() or module.instanceCall()
return {
type: 'property_access',
type: 'call',
left: {
type: 'static_or_instance_call',
id: id,
propertyPath: propertyPath
},
args: args,
loc: {
start: id.loc.start,
end: this.lexer.loc()
},
};
}

if (this.look.tag === '[') {
this.move();
let accessKey = this.expr();
this.match(']');
// @id.x[]
return this.mapAccess({
type: 'map_access',
id: id,
propertyPath: propertyPath,
accessKey: accessKey,
loc: {
start: id.loc.start,
end: this.lexer.loc()
}
});
}

// id.x = xxx
if (this.look.tag === '=') {
this.move();
let expr = this.expr();
return {
type: 'assign',
left: {
type: 'property',
id,
propertyPath
},
expr: expr
};
}

// id.x
return {
type: 'variable',
type: 'property_access',
id: id,
loc: id.loc
propertyPath: propertyPath,
loc: {
start: id.loc.start,
end: this.lexer.loc()
}
};
}

Expand Down Expand Up @@ -1766,6 +1775,10 @@ class Parser extends BaseParser {
const item = this.exprItem();
let end = this.getIndex();

if (this.is('.')) {
return this.properties(item);
}

if (this.is('+')) {
this.move();
const right = this.expr();
Expand Down
15 changes: 12 additions & 3 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
isNumber,
isCompare,
isInteger,
isTmpVariable,
getDarafile
} = require('./util');

Expand Down Expand Up @@ -1651,7 +1652,6 @@ class TypeChecker {
if (id.tag === Tag.VID) {
return this.checkVid(id, env);
}

// 未定义变量检查
const name = id.lexeme;
if (env.local && env.local.hasDefined(name)) {
Expand Down Expand Up @@ -1683,6 +1683,11 @@ class TypeChecker {
id.type = 'enum';
return;
}

if (isTmpVariable(id.type)) {
return;
}

this.error(`variable "${name}" undefined`, id);
}

Expand Down Expand Up @@ -2340,8 +2345,8 @@ class TypeChecker {
if (ast.left.type === 'static_or_instance_call') {
const id = ast.left.id;
this.checkId(id, env);
if (env.local.hasDefined(id.lexeme)) {

if (env.local.hasDefined(id.lexeme) || isTmpVariable(id.type)) {
ast.left.type = 'instance_call';
this.visitInstanceCall(ast, env);
return;
Expand Down Expand Up @@ -2572,6 +2577,10 @@ class TypeChecker {
return _basic('class');
}

if (isTmpVariable(id.type)) {
return this.getExprType(id, env);
}

console.log(id);
throw new Error('Can not get the type for variable');
}
Expand Down
9 changes: 9 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ exports.isCompare = function (type){
return compareType.indexOf(type) !== -1;
};

exports.isTmpVariable = function(type) {
const tmpType = [
'template_string', 'array', 'object',
'group', 'boolean', 'number', 'string'
];

return tmpType.indexOf(type) !== -1;
};

exports.isNumber = function (type) {
const numberType = [
'number', 'integer', 'int8', 'int16',
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/tmp_var_call/Darafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
9 changes: 9 additions & 0 deletions test/fixtures/tmp_var_call/main.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
init(){
var arr = 'str.abc'.split('.');
var str = ['1','2','3'].join('.');
var num = 1.parseLong();
var en = {
a = "b"
}.keySet();
}

1 change: 1 addition & 0 deletions test/note.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('note util', function () {
},
'arg': {
'type': 'array',
'loc': loc(17, 8, 17, 75),
'items': [
{
'type': 'property_access',
Expand Down
39 changes: 38 additions & 1 deletion test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2320,7 +2320,7 @@ describe('parser', function () {
expr('{a = 1, b = 2.}');
}).to.throwException(function (e) { // get the exception object
expect(e).to.be.a(SyntaxError);
expect(e.message).to.be(`Unexpected token: .. expect ","`);
expect(e.message).to.be(`Unexpected token: }. Expect ID, but }`);
});

expect(() => {
Expand Down Expand Up @@ -9892,6 +9892,42 @@ describe('parser', function () {
});
});

it('tmp variable method call should be ok', function () {
var ast = parse(`
init(){
var str = 'tmpVar'.empty();
}
`, '__filename');
console.log('%j', ast);
const expr = ast.moduleBody.nodes[0].initBody.stmts[0].expr;
expect(expr).to.be.eql({
'type': 'call',
'left': {
'type': 'static_or_instance_call',
'id': {
'type': 'string',
'value': {
'tag': 1,
'loc': loc(3, 20, 3, 26),
'string': 'tmpVar',
'index': 8
},
'loc': loc(3, 20, 3, 26),
},
'propertyPath': [
{
'tag': 2,
'loc': loc(3, 28, 3, 33),
'lexeme': 'empty',
'index': 10
}
]
},
'args': [],
'loc': loc(3, 20, 3, 35),
});
});

it('exception filed attrs should be ok', function () {
function exceptionFieldAttrs(value) {
var ast = parse(`
Expand Down Expand Up @@ -10262,6 +10298,7 @@ describe('parser', function () {
},
'arg': {
'type': 'array',
'loc': loc(8, 12, 8, 26),
'items': [
{
'type': 'string',
Expand Down
6 changes: 6 additions & 0 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7682,4 +7682,10 @@ init() {
}
});
});

it('use tmp variable method call shoule be ok', function(){
expect(function () {
readAndParse('fixtures/tmp_var_call/main.dara');
}).to.not.throwException();
});
});

0 comments on commit 99b0905

Please sign in to comment.