Skip to content

Commit

Permalink
Replace the type annotation with an action for building string nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jun 13, 2017
1 parent 6da04c7 commit 91ad7e3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
29 changes: 29 additions & 0 deletions src/ast/string.js
@@ -0,0 +1,29 @@
'use strict';

var util = require('../util');

var String = function(text) {
this._text = text;
this._value = eval(text);
};

util.assign(String.prototype, {
toSexp: function() {
return ['string', this._value];
},

compile: function(builder, address, action) {
var value = this._value,
length = value.length,
chunk = builder.chunk_(length);

builder.if_(builder.stringMatch_(chunk, value), function(builder) {
var of = builder.offset_();
builder.syntaxNode_(address, of, of + ' + ' + length, null, action);
}, function(builder) {
builder.failure_(address, this._text);
}, this);
}
});

module.exports = String;
13 changes: 10 additions & 3 deletions src/compiler.js
Expand Up @@ -17,8 +17,15 @@ var types = {
Reference: require('./compiler/reference'),
Repeat: require('./compiler/repeat'),
Sequence: require('./compiler/sequence'),
SequencePart: require('./compiler/sequence_part'),
String: require('./compiler/string')
SequencePart: require('./compiler/sequence_part')
};

var String = require('./ast/string');

var actions = {
string: function(text, a, b, elements) {
return new String(text.substring(a, b));
}
};

var Compiler = function(grammarText, builder) {
Expand All @@ -30,7 +37,7 @@ util.assign(Compiler.prototype, {
parseTree: function() {
if (this._tree) return this._tree;

this._tree = metagrammar.parse(this._grammarText, {types: types});
this._tree = metagrammar.parse(this._grammarText, {types: types, actions: actions});
if (this._tree) return this._tree;

var message = metagrammar.formatError(metagrammar.Parser.lastError);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/grammar.js
Expand Up @@ -16,7 +16,8 @@ module.exports = {
compile: function(builder) {
var scan = function(node, callback, context) {
callback.call(context, node);
node.forEach(function(child) { scan(child, callback, context) });
if (node.forEach)
node.forEach(function(child) { scan(child, callback, context) });
};

builder.package_(this.grammarName(), function(builder) {
Expand Down
20 changes: 0 additions & 20 deletions src/compiler/string.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/meta_grammar.js
Expand Up @@ -1837,7 +1837,7 @@
if (elements0 === null) {
address0 = FAILURE;
} else {
address0 = new TreeNode(this._input.substring(index2, this._offset), index2, elements0);
address0 = this._actions.string(this._input, index2, this._offset, elements0);
this._offset = this._offset;
}
if (address0 === FAILURE) {
Expand Down Expand Up @@ -1989,14 +1989,13 @@
if (elements3 === null) {
address0 = FAILURE;
} else {
address0 = new TreeNode(this._input.substring(index6, this._offset), index6, elements3);
address0 = this._actions.string(this._input, index6, this._offset, elements3);
this._offset = this._offset;
}
if (address0 === FAILURE) {
this._offset = index1;
}
}
extend(address0, this._types.String);
this._cache._string_expression[index0] = [address0, this._offset];
return address0;
},
Expand Down
3 changes: 2 additions & 1 deletion src/meta_grammar.peg
Expand Up @@ -57,7 +57,8 @@ predicated_atom <- predicate:("&" / "!") atom <Predicate>

reference_expression <- identifier !assignment <Reference>

string_expression <- ('"' ("\\" . / [^"])* '"' / "'" ("\\" . / [^'])* "'") <String>
string_expression <- '"' ("\\" . / [^"])* '"' %string
/ "'" ("\\" . / [^'])* "'" %string

ci_string_expression <- "`" ("\\" . / [^`])* "`" <CIString>

Expand Down

0 comments on commit 91ad7e3

Please sign in to comment.