Skip to content

Commit

Permalink
[api] Improved comment handling in lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Sep 2, 2010
1 parent 276166f commit 4d12d94
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
test/output/*.js
test/output/*.js
test/beta-test.js
2 changes: 1 addition & 1 deletion examples/simple.feature
@@ -1,4 +1,4 @@
Feature: Addition
Feature: Addition #AND A COMMENT!!!
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
Expand Down
38 changes: 30 additions & 8 deletions lib/kyuri/lexer.js
Expand Up @@ -16,7 +16,8 @@ var MULTI_DENT = /^(\t+)(\.)?/,
PYSTRING = /"""/,
TAG = /@(\w+)/i,
COMMENT = /#\s*([\S+\s+]+)/i,
SENTENCE = /([\S+\s+]+)/i;
SENTENCE = /([\S+\s+]+)/i,
SENTENCE_COMMENT = /([\S+\s+]+)#\s*([\S+\s+]+)/i;

var Lexer = function (language, i18n) {
// Set the i18n dictionary on the lexer for later use
Expand Down Expand Up @@ -92,9 +93,9 @@ Lexer.prototype = {
},

extractNextToken: function () {
var tokens = ['lineToken', 'commentToken', 'featureToken', 'scenarioToken', 'scenarioOutlineToken',
'backgroundToken', 'examplesToken', 'examplesRowToken', 'operatorToken', 'pystringToken',
'tagToken', 'sentenceToken'];
var tokens = ['lineToken', 'featureToken', 'scenarioToken', 'scenarioOutlineToken',
'backgroundToken', 'commentToken', 'examplesToken', 'examplesRowToken', 'operatorToken',
'pystringToken', 'tagToken', 'sentenceToken'];

for (var index in tokens) {
if (this[tokens[index]].apply(this)) {
Expand Down Expand Up @@ -175,8 +176,29 @@ Lexer.prototype = {
return this.complexMatch(TAG, 'TAG');
},

commentToken: function() {
return this.complexMatch(COMMENT, 'COMMENT');
commentToken: function () {
var match;
if (!(match = this.match(COMMENT))) {
return false;
}

// If the comment is at the beginning of the chunk
// then evaluate it normally
if(this.chunk.indexOf('#') === 0) {
this.i += match[0].length;
this.token('COMMENT', match[1].trim());
return true;
}

// Otherwise we have a comment inside of
// a SENTENCE token, so strip off the SENTENCE
// and create tokens for both.
match = this.match(SENTENCE_COMMENT);
this.i += match[0].length;
this.token('SENTENCE', match[1].trim());
this.token('COMMENT', match[2].trim());

return true;
},

sentenceToken: function () {
Expand All @@ -186,7 +208,7 @@ Lexer.prototype = {
}

this.i += sentence.length;
this.token('SENTENCE', sentence);
this.token('SENTENCE', sentence.trim());

return true;
},
Expand Down Expand Up @@ -252,7 +274,7 @@ Lexer.prototype = {
return true;
}

this.token(token, match[0]);
this.token(token, match[0].trim());
return true;
},

Expand Down

0 comments on commit 4d12d94

Please sign in to comment.