Skip to content

Commit

Permalink
update deps, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Escalante committed Jun 10, 2019
1 parent a5da5bc commit ca81792
Show file tree
Hide file tree
Showing 7 changed files with 1,331 additions and 5,851 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const lex = require('./lexer')
const parse = require('./parser')

module.exports = function SugarMLParser(input, options) {
return parse(lex(input, options),input,options)
return parse(lex(input, options), input, options)
}

module.exports.lex = lex
Expand Down
18 changes: 13 additions & 5 deletions lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ module.exports = function Lexer(input, options = {}) {
// - if it has a value, it will end at the *=*
// - if it's boolean, it will end at the *paren* or *space* (next attr)
const key = collectUntil('=|)|\\s/')

// if we do have a key, add it to our tokens, otherwise if we have a space or invalid '/' char in attr name move on (example : attr = 'foo')
if (key.length) {
addToken('attributeKey', key)
} else if (char && (char.match(/\s/) || char.match(/\//)) && !char.match(/\n/)) {
} else if (
char &&
(char.match(/\s/) || char.match(/\//)) &&
!char.match(/\n/)
) {
next()
}

Expand Down Expand Up @@ -202,7 +206,11 @@ module.exports = function Lexer(input, options = {}) {
if (char && char.match(/\s/) && !char.match(/\n/)) next()

//If we run out of chars or char or next char are a new line when attribute list isn't closed, that's a syntax error
if (!char || char.match(/\n/) || (char !== ')' && nextChar() && nextChar().match(/\n/))) {
if (
!char ||
char.match(/\n/) ||
(char !== ')' && nextChar() && nextChar().match(/\n/))
) {
throw new SugarmlError({
message: `Unclosed attribute parentheses`,
location: {
Expand All @@ -213,7 +221,6 @@ module.exports = function Lexer(input, options = {}) {
}
})
}

}
// done with attributes, move past the *close paren*
next()
Expand Down Expand Up @@ -293,7 +300,8 @@ module.exports = function Lexer(input, options = {}) {

// if the previous token was a tag, it's a space before content or
// trailing whitespace on an empty tag
if (lastToken &&
if (
lastToken &&
['tag', 'attributeKey', 'attributeValue'].indexOf(lastToken.type) > -1
) {
// move past the space
Expand Down
36 changes: 18 additions & 18 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const SugarmlError = require('./error')

// Our target output is a reshape AST
// (https://github.com/reshape/reshape#reshape-ast)
module.exports = (tokens,input,options={}) => {
module.exports = (tokens, input, options = {}) => {
let current = 0
let indentLevel = 0
let token = tokens[current]

function walk(ctx) {
token = tokens[current]

Expand Down Expand Up @@ -85,9 +85,9 @@ module.exports = (tokens,input,options={}) => {
function comment() {
if (token && token.type === 'comment') {
current++
if (token.value.startsWith('-')){
if (token.value.startsWith('-')) {
// TODO figure out better way to not push anything
return null;
return null
}
return {
type: 'comment',
Expand Down Expand Up @@ -151,29 +151,29 @@ module.exports = (tokens,input,options={}) => {
// searching for contents
const currentIndent = indentLevel

var forbiddenChildren=false
var inlineText=''
var forbiddenChildren = false
var inlineText = ''
//If next token is 'text', we must not have child tokens
if (token && token.type==='text'){
inlineText = token.value; //Remember inline text value for futur use (in case of error)
if (token && token.type === 'text') {
inlineText = token.value //Remember inline text value for futur use (in case of error)
node.content.push(walk(node.content)) //Get text
forbiddenChildren=true
forbiddenChildren = true
}

// now we recurse to get the contents, looping while the indent level is
// greater than that of the current node, to pick up everything nested
node.content.push(walk(node.content))
if (forbiddenChildren && indentLevel > currentIndent){
if (forbiddenChildren && indentLevel > currentIndent) {
let errorLine
if (token){
errorLine = tokens[current-2].line; //With other token to parse, error line is 2 tokens before
}else{
errorLine = tokens[current-1].line; //Without anymore tokens to parse, error is on the last line
if (token) {
errorLine = tokens[current - 2].line //With other token to parse, error line is 2 tokens before
} else {
errorLine = tokens[current - 1].line //Without anymore tokens to parse, error is on the last line
}
//Compute the awaited input
let splited = input.split('\n') //Split input line by line
let regex = inlineText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '$'; //Make regex from inlineText
let firstLine = splited[errorLine - 2].replace(new RegExp(regex),'') //Remove inlineText from first Error line
let regex = inlineText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '$' //Make regex from inlineText
let firstLine = splited[errorLine - 2].replace(new RegExp(regex), '') //Remove inlineText from first Error line
let thirdLine = splited[errorLine - 1] //Second line become third
let secondLine = thirdLine.match(/^\s+/)[0] + '| ' + inlineText //Second line is *pipe* + inlineText we removed on first error line
let shouldBe = ` 1 | ${firstLine}\n 2 | ${secondLine}\n 3 | ${thirdLine}\n` //Pretty output
Expand Down Expand Up @@ -249,7 +249,7 @@ module.exports = (tokens,input,options={}) => {
// TODO figure out better way to not push anything
return null
}
}else if (token && token.type === 'indent') {
} else if (token && token.type === 'indent') {
//Ignore indent at start of file
next()
// TODO figure out better way to not push anything
Expand Down
Loading

0 comments on commit ca81792

Please sign in to comment.