Skip to content

Commit

Permalink
Add "mixed" and "object" types
Browse files Browse the repository at this point in the history
  • Loading branch information
asmblah committed May 28, 2023
1 parent f97d267 commit 2bd7554
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/grammar.js
Expand Up @@ -2077,9 +2077,15 @@ module.exports = {
'N_ITERABLE_TYPE': {
components: [{allowMerge: false, what: /iterable\b/i}]
},
'N_MIXED_TYPE': {
components: [{allowMerge: false, what: /mixed\b/i}]
},
'N_NULL_TYPE': {
components: [{allowMerge: false, what: /null\b/i}]
},
'N_OBJECT_TYPE': {
components: [{allowMerge: false, what: /object\b/i}]
},
'N_TYPE': {
components: [
{oneOf: ['N_UNION_TYPE', 'N_NON_UNION_TYPE']}
Expand Down Expand Up @@ -2119,7 +2125,9 @@ module.exports = {
'N_ARRAY_TYPE',
'N_CALLABLE_TYPE',
'N_ITERABLE_TYPE',
'N_MIXED_TYPE',
'N_NULL_TYPE',
'N_OBJECT_TYPE',
'N_SCALAR_TYPE',
'N_VOID_TYPE',
'N_CLASS_TYPE'
Expand Down
57 changes: 57 additions & 0 deletions test/integration/types/mixedTypeTest.js
@@ -0,0 +1,57 @@
/*
* PHP-To-AST - PHP parser
* Copyright (c) Dan Phillimore (asmblah)
* http://uniter.github.com/phptoast/
*
* Released under the MIT license
* https://github.com/uniter/phptoast/raw/master/MIT-LICENSE.txt
*/

'use strict';

var _ = require('microdash'),
expect = require('chai').expect,
tools = require('../../tools');

describe('PHP Parser grammar mixed type integration', function () {
var parser;

beforeEach(function () {
parser = tools.createParser();
});

_.each({
'empty function definition with explicit mixed return type': {
code: 'function myFunc() : mixed {}',
expectedAST: {
name: 'N_PROGRAM',
statements: [{
name: 'N_FUNCTION_STATEMENT',
func: {
name: 'N_STRING',
string: 'myFunc'
},
args: [],
body: {
name: 'N_COMPOUND_STATEMENT',
statements: []
},
returnType: {
name: 'N_MIXED_TYPE'
}
}]
}
}
}, function (scenario, description) {
describe(description, function () {
var code = '<?php ' + scenario.code;

// Pretty-print the code strings so any non-printable characters are readable.
describe('when the code is ' + JSON.stringify(code) + ' ?>', function () {
it('should return the expected AST', function () {
expect(parser.parse(code)).to.deep.equal(scenario.expectedAST);
});
});
});
});
});
57 changes: 57 additions & 0 deletions test/integration/types/objectTypeTest.js
@@ -0,0 +1,57 @@
/*
* PHP-To-AST - PHP parser
* Copyright (c) Dan Phillimore (asmblah)
* http://uniter.github.com/phptoast/
*
* Released under the MIT license
* https://github.com/uniter/phptoast/raw/master/MIT-LICENSE.txt
*/

'use strict';

var _ = require('microdash'),
expect = require('chai').expect,
tools = require('../../tools');

describe('PHP Parser grammar object type integration', function () {
var parser;

beforeEach(function () {
parser = tools.createParser();
});

_.each({
'empty function definition with explicit object return type': {
code: 'function myFunc() : object {}',
expectedAST: {
name: 'N_PROGRAM',
statements: [{
name: 'N_FUNCTION_STATEMENT',
func: {
name: 'N_STRING',
string: 'myFunc'
},
args: [],
body: {
name: 'N_COMPOUND_STATEMENT',
statements: []
},
returnType: {
name: 'N_OBJECT_TYPE'
}
}]
}
}
}, function (scenario, description) {
describe(description, function () {
var code = '<?php ' + scenario.code;

// Pretty-print the code strings so any non-printable characters are readable.
describe('when the code is ' + JSON.stringify(code) + ' ?>', function () {
it('should return the expected AST', function () {
expect(parser.parse(code)).to.deep.equal(scenario.expectedAST);
});
});
});
});
});

0 comments on commit 2bd7554

Please sign in to comment.