Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add parsing for members declaration in a class

  • Loading branch information...
commit 2d2e980027dae81274d9ab259b784b218c23596b 1 parent c9bb174
@mwilliamson authored
View
7 lib/nodes.js
@@ -95,6 +95,13 @@ exports.formalTypeParameter = function(name, source) {
}, source);
};
+exports.memberDeclarationByReference = function(name, source) {
+ return node({
+ nodeType: "memberDeclarationByReference",
+ name: name
+ }, source);
+};
+
exports.assign = function(variable, value, source) {
return node({
nodeType: "assignment",
View
33 lib/parsing/expressions.js
@@ -181,9 +181,37 @@ var lambdaRule = lazyRule(function() {
);
});
+var memberRule = lazyRule(function() {
+ return then(
+ identifier(),
+ nodes.memberDeclarationByReference
+ );
+});
+
+var membersRule = lazyRule(function() {
+ var members = capture(zeroOrMoreWithSeparator(memberRule, symbol(",")), "members");
+ return then(
+ sequence(
+ keyword("members"),
+ symbol("{"),
+ members,
+ symbol("}")
+ ),
+ sequence.extract(members)
+ );
+});
+
+var optionalMembersRule = then(
+ optional(membersRule),
+ function(members) {
+ return members.orElse([]);
+ }
+);
+
var classRule = lazyRule(function() {
var formalTypeParameters = capture(optional(formalTypeParametersRule), "formalTypeParameters");
var formalArguments = capture(formalArgumentsRule, "formalArguments");
+ var members = capture(optionalMembersRule, "members");
var body = capture(zeroOrMore(statements.statement), "body");
return then(
sequence(
@@ -193,12 +221,11 @@ var classRule = lazyRule(function() {
formalArguments,
symbol("=>"),
symbol("{"),
+ members,
body,
symbol("}")
),
- applySequenceValuesToNode(function(formalTypeParameters, formalArguments, body, source) {
- return nodes.class(formalTypeParameters, formalArguments, [], body, source);
- }, formalTypeParameters, formalArguments, body)
+ applySequenceValuesToNode(nodes.class, formalTypeParameters, formalArguments, members, body)
);
});
View
12 test/parsing/expressions.test.js
@@ -190,6 +190,18 @@ exports.canParseClassDefinitionWithBody = function(test) {
test.done();
};
+exports.canParseClassDefinitionWithMembersDeclaration = function(test) {
+ var result = parse(parsing.expression, "class() => { members { blah } }");
+ var expected = nodes.class(
+ options.none,
+ nodes.formalArguments([]),
+ [nodes.memberDeclarationByReference("blah")],
+ []
+ );
+ assertIsSuccessWithValue(test, result, ignoringSources(expected));
+ test.done();
+};
+
exports.canParseEmptyObject = function(test) {
var result = parse(parsing.expression, "object { }");
var expected = nodes.object([]);
Please sign in to comment.
Something went wrong with that request. Please try again.