Skip to content

Commit

Permalink
jsxelemname lexer允许name:name,初步实现attr #53
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Apr 18, 2015
1 parent b8af600 commit 7bc94cb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/lexer/JSXLexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var walk = require('../util/walk');
var RegMatch = require('./match/RegMatch');
var CompleteEqual = require('./match/CompleteEqual');
var LineSearch = require('./match/LineSearch');
var CharacterSet = require('./match/CharacterSet');

var ELEM = new RegMatch(JSXToken.ELEM, /^[a-z]\w*(?:-\w+)*/i);
var JSXMatch = [
Expand All @@ -17,7 +18,7 @@ var JSXMatch = [
new CompleteEqual(JSXToken.SIGN, character.DECIMAL),
new LineSearch(JSXToken.STRING, '"', '"', true),
new LineSearch(JSXToken.STRING, "'", "'", true),
new CompleteEqual(JSXToken.SIGN, '=', null, true),
new CharacterSet(JSXToken.SIGN, '=:'),
new RegMatch(JSXToken.NUMBER, /^\d+(?:\.\d*)?/),
new RegMatch(JSXToken.PROPERTY, /^[a-z]+(?:-\w+)*/i)
];
Expand Down
3 changes: 3 additions & 0 deletions src/parser/jsx/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var Node = Es6Node.extend(function(type, children) {
JSXOpeningElement: 'JSXOpeningElement',
JSXClosingElement: 'JSXClosingElement',
JSXChild: 'JSXChild',
JSXSpreadAttribute: 'JSXSpreadAttribute',
JSXAttribute: 'JSXAttribute',
JSXNamespacedName: 'JSXNamespacedName',
getKey: function(s) {
if(!s) {
throw new Error('empty value');
Expand Down
47 changes: 42 additions & 5 deletions src/parser/jsx/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ var Parser = Es6Parser.extend(function(lexer) {
this.match(),
this.jsxelemname()
);
while(this.look && this.look.type() == Token.PROPERTY) {
node.add(this.property());
while(this.look
&&
(this.look.type() == Token.PROPERTY
|| this.look.content() == '{')) {
node.add(this.attr());
}
if(!this.look) {
this.error();
Expand All @@ -46,11 +49,46 @@ var Parser = Es6Parser.extend(function(lexer) {
//TODO: JSXElementName
return this.match(Token.ELEM);
},
property: function() {
attr: function() {
if(this.look.content() == '{') {
return this.spreadattr();
}
//TODO: JSXElementName
var node = new Node(Node.JSXElementName);
var node = new Node(Node.JSXAttribute);
node.add(
this.attrname(),
this.match('='),
this.attrval()
);
return node;
},
spreadattr: function() {
//TODO: JSXSpreadAttribute
},
attrname: function() {
var id = this.match(Token.PROPERTY);
if(this.look && this.look.content() == ':') {
var node = new Node(Node.JSXNamespacedName);
node.add(id,
this.match(),
this.match(Token.PROPERTY)
);
return node;
}
return id;
},
attrval: function() {
if(!this.look) {
this.error();
}
if(this.look.content() == '{') {
//TODO: { AssignmentExpression }
}
else if(this.look.type() == Token.STRING) {
return this.match();
}
return this.jsxelem();
},
jsxchild: function() {
switch(this.look.type()) {
case Token.TEXT:
Expand All @@ -72,7 +110,6 @@ var Parser = Es6Parser.extend(function(lexer) {
)
return node;
},

match: function(type, line, msg) {
if(typeof type == 'boolean') {
msg = line;
Expand Down
3 changes: 2 additions & 1 deletion web/lexer/JSXLexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var walk = require('../util/walk');
var RegMatch = require('./match/RegMatch');
var CompleteEqual = require('./match/CompleteEqual');
var LineSearch = require('./match/LineSearch');
var CharacterSet = require('./match/CharacterSet');

var ELEM = new RegMatch(JSXToken.ELEM, /^[a-z]\w*(?:-\w+)*/i);
var JSXMatch = [
Expand All @@ -17,7 +18,7 @@ var JSXMatch = [
new CompleteEqual(JSXToken.SIGN, character.DECIMAL),
new LineSearch(JSXToken.STRING, '"', '"', true),
new LineSearch(JSXToken.STRING, "'", "'", true),
new CompleteEqual(JSXToken.SIGN, '=', null, true),
new CharacterSet(JSXToken.SIGN, '=:'),
new RegMatch(JSXToken.NUMBER, /^\d+(?:\.\d*)?/),
new RegMatch(JSXToken.PROPERTY, /^[a-z]+(?:-\w+)*/i)
];
Expand Down
3 changes: 3 additions & 0 deletions web/parser/jsx/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var Node = Es6Node.extend(function(type, children) {
JSXOpeningElement: 'JSXOpeningElement',
JSXClosingElement: 'JSXClosingElement',
JSXChild: 'JSXChild',
JSXSpreadAttribute: 'JSXSpreadAttribute',
JSXAttribute: 'JSXAttribute',
JSXNamespacedName: 'JSXNamespacedName',
getKey: function(s) {
if(!s) {
throw new Error('empty value');
Expand Down
47 changes: 42 additions & 5 deletions web/parser/jsx/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ var Parser = Es6Parser.extend(function(lexer) {
this.match(),
this.jsxelemname()
);
while(this.look && this.look.type() == Token.PROPERTY) {
node.add(this.property());
while(this.look
&&
(this.look.type() == Token.PROPERTY
|| this.look.content() == '{')) {
node.add(this.attr());
}
if(!this.look) {
this.error();
Expand All @@ -46,11 +49,46 @@ var Parser = Es6Parser.extend(function(lexer) {
//TODO: JSXElementName
return this.match(Token.ELEM);
},
property: function() {
attr: function() {
if(this.look.content() == '{') {
return this.spreadattr();
}
//TODO: JSXElementName
var node = new Node(Node.JSXElementName);
var node = new Node(Node.JSXAttribute);
node.add(
this.attrname(),
this.match('='),
this.attrval()
);
return node;
},
spreadattr: function() {
//TODO: JSXSpreadAttribute
},
attrname: function() {
var id = this.match(Token.PROPERTY);
if(this.look && this.look.content() == ':') {
var node = new Node(Node.JSXNamespacedName);
node.add(id,
this.match(),
this.match(Token.PROPERTY)
);
return node;
}
return id;
},
attrval: function() {
if(!this.look) {
this.error();
}
if(this.look.content() == '{') {
//TODO: { AssignmentExpression }
}
else if(this.look.type() == Token.STRING) {
return this.match();
}
return this.jsxelem();
},
jsxchild: function() {
switch(this.look.type()) {
case Token.TEXT:
Expand All @@ -72,7 +110,6 @@ var Parser = Es6Parser.extend(function(lexer) {
)
return node;
},

match: function(type, line, msg) {
if(typeof type == 'boolean') {
msg = line;
Expand Down

0 comments on commit 7bc94cb

Please sign in to comment.