0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-12 11:02:42 +00:00

The jsx parser is finally working

This commit is contained in:
Scott Tolksdorf
2016-04-01 13:09:00 -04:00
parent 536a768133
commit e61e1a698d

View File

@@ -6,7 +6,7 @@ var LETTERS = /[a-zA-Z_]/;
var tokenMap = { var tokenMap = {
//'<' : 'brace', //'<' : 'brace',
//'>' : 'brace', //'>' : 'brace',
'/' : 'close', //'/' : 'close',
'=' : 'equals', '=' : 'equals',
} }
@@ -29,112 +29,104 @@ var tokenizer = function(input){
return value; return value;
} }
if(char == '>'){ if(inTag){
inTag = false; if(char == '>'){
tokens.push({ inTag = false;
type : 'closeTag' tokens.push({
}) type : 'closeTag'
current++; })
continue;
}
else if(char == '/' && input[current+1] == '>'){
inTag = false;
tokens.push({
type : 'endTag'
})
current++;
}
else if(char == '='){
tokens.push({
type : 'equals'
});
}
else if(WHITESPACE.test(char)){
}
else if(NUMBERS.test(char)){
tokens.push({
type : 'number',
value : getToken(NUMBERS)*1
});
continue;
}
else if(LETTERS.test(char)){
tokens.push({
type : 'word',
value : getToken(LETTERS)
});
continue;
}
else if(char == "'"){
char = input[++current]
tokens.push({
type : 'text',
value : getToken(/[^\']/)
});
}
else if(char == '"'){
char = input[++current]
tokens.push({
type : 'text',
value : getToken(/[^\"]/)
});
}
} }
if(char == '/' && input[current+1] == '>'){ //Not in a tag def
inTag = false; else{
tokens.push({
type : 'endTag'
})
current++;
current++;
continue;
}
//End tag
if(tokenMap[char]){ if(char == '<' && input[current+1] == '/'){
tokens.push({
type : tokenMap[char],
value : char
});
current++;
continue;
}
if(WHITESPACE.test(char)){
current++;
continue;
}
if(NUMBERS.test(char)){
tokens.push({
type : 'number',
value : getToken(NUMBERS)*1
});
continue;
}
if(LETTERS.test(char)){
tokens.push({
type : 'word',
value : getToken(LETTERS)
});
continue;
}
if(char == "'"){
char = input[++current]
tokens.push({
type : 'word',
value : getToken(/[^\']/)
});
current++;
continue;
}
if(char == '"'){
char = input[++current]
tokens.push({
type : 'word',
value : getToken(/[^\"]/)
});
current++;
continue;
}
if(char == '<'){
inTag = true;
if(input[current+1] == '/'){
char = input[++current] char = input[++current]
char = input[++current] char = input[++current]
//parse end element
//console.log(char, getToken(LETTERS));
tokens.push({ tokens.push({
type : 'endTag', type : 'endTag',
value : getToken(LETTERS) value : getToken(LETTERS)
}) })
current++; //current++;
}else{ }
else if(char == '<'){
inTag = true;
char = input[++current]; char = input[++current];
tokens.push({ tokens.push({
type : 'openTag', type : 'openTag',
value : getToken(LETTERS) value : getToken(LETTERS)
}) })
console.log(char);
current--;
} }
current++; else{
continue; //Handle slush text
var value = '';
while(char != '<' && current < input.length){
value += char;
char = input[++current];
}
value = value.trim()
if(value){
tokens.push({
type : 'text',
value : value
});
}
current--;
}
} }
//Handle slush text current++;
var value = '';
while(char != '<' && current < input.length){
value += char;
char = input[++current];
}
value = value.trim()
if(value){
tokens.push({
type : 'text',
value : value
});
}
continue; continue;
} }
@@ -182,39 +174,30 @@ var parser = function(tokens){
} }
var genNode = function(type){ var genNode = function(tagType){
token = tokens[++current]; token = tokens[++current];
var node = { var node = {
type : type, tag : tagType,
props : getProps(), props : getProps(),
children : [] children : getChildren(tagType)
} }
//grab props
//search for children
//return them
var children = [];
while(token.type != 'endTag' && token.value != 'type' && current < tokens.length){
if(token.type == 'openTag'){
children.push(genNode(token.value));
}
token = tokens[++current];
}
node.children = children;
return node return node
} }
var getChildren = function(tagType){
var children = [];
while(current < tokens.length && token.type != 'endTag' && token.value != tagType){
if(token.type == 'openTag'){
children.push(genNode(token.value));
}else if(token.type == 'text'){
children.push(token.value);
}
token = tokens[++current];
}
return children;
}
//get an open tag return getChildren();
//grab the props
return getProps();
} }
@@ -222,25 +205,30 @@ var parser = function(tokens){
var test1 = `
why you so cray
var tokens = tokenizer(`
<div test="here there 'champ'" more_cool size=0> <div test="here there 'champ'" more_cool size=0>
<span></span> <span>Hey there!<a>so fucking cool</a></span>
let's go party!@
we be cray
</div> </div>
<a href='neato'></a> <a href='neato' />
`); `
var test2 = "<div>Hey there!</div>"
var tokens = tokenizer(test1);
console.log(tokens); console.log(tokens);
console.log(parser(tokens)); console.log(test1, JSON.stringify(parser(tokens), null, ' '));