Skip to content

Commit

Permalink
Add new parser implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfeedface committed Nov 9, 2013
1 parent 535d192 commit f1e1a02
Show file tree
Hide file tree
Showing 23 changed files with 1,140 additions and 1,469 deletions.
3 changes: 3 additions & 0 deletions .lldbinit
@@ -0,0 +1,3 @@
target create node
breakpoint set --file parser_wrapper.h --line 35
process launch examples/stream.js
44 changes: 30 additions & 14 deletions binding.gyp
@@ -1,20 +1,36 @@
{ "targets": [ { { "targets": [ {
"target_name": "bindings", "target_name": "bindings",
"variables": { "variables": {
"raptor_prefix": "/usr/local" "raptor_prefix": "/usr/local"
}, },
"include_dirs": [ "include_dirs": [
"<(raptor_prefix)/include/raptor2" "<(raptor_prefix)/include/raptor2"
], ],
"sources": [ "sources": [
"src/bindings.cc", "src/bindings.cc",
"src/parser.cc", "src/parser.cc",
"src/serializer.cc", "src/parser_wrapper.cc",
"src/statement.cc" "src/statement.cc",
], "src/statement_wrapper.cc",
"link_settings": { "src/uri.cc",
"src/world.cc",
"src/message.cc"
],
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-std=c++11", "-fno-exceptions" ],
"link_settings": {
"libraries": [ "-lraptor2" ] "libraries": [ "-lraptor2" ]
} },
} "conditions": [ [
] "OS=='mac'", {
} "xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"OTHER_CPLUSPLUSFLAGS": [
"-std=c++11",
"-stdlib=libc++",
"-mmacosx-version-min=10.7"
]
}
}
] ]
} ] }
83 changes: 55 additions & 28 deletions index.js
@@ -1,37 +1,64 @@
var events = require('events'), var stream = require('stream'),
util = require('util'), bindings;
bindings = require(__dirname + '/build/Release/bindings');


function inherits(target, source) { bindings = require(__dirname + '/build/Release/bindings');
for (var key in source.prototype) { // bindings = require(__dirname + '/build/Debug/bindings');
target[key] = source.prototype[key];
}
}


exports.newParser = function (mimeType, cb) { exports.createParser = function (syntax) {
if (undefined === cb) { syntax = syntax || 'guess';
var parser = bindings.newParser(mimeType); var parser = new bindings.Parser(syntax);
inherits(parser, events.EventEmitter); return new StreamParser(parser);
return parser;
} else {
var res = bindings.newParser(mimeType, function (parser) {
inherits(parser, events.EventEmitter);
cb(parser);
});
}
}; };


function StreamParser(parser) {
stream.Transform.call(this);
this._parser = parser;
this._started = false;

var self = this;
this._parser.setStatementHandler(function (statement) {
self.emit('statement', statement);
});
this._parser.setNamespaceHandler(function (namespaceURI, prefix) {
self.emit('namespace', namespaceURI, prefix);
});
this._parser.setMessageHandler(function (type, message) {
self.emit('message', {
type: type,
text: message
});
});
}

StreamParser.prototype = Object.create(stream.Transform.prototype);


exports.newSerializer = function (mimeType) { StreamParser.prototype.setBaseURI = function (baseURI) {
var serializer = null; this._baseURI = baseURI;
return this;
};


if (undefined === mimeType) { StreamParser.prototype._transform = function (chunk, encoding, cb) {
serializer = bindings.newSerializer(); try {
} else { if (!this._started) {
serializer = bindings.newSerializer(mimeType); if (!this._baseURI) {
throw RangeError('base URI not set');
}
this._parser.parseStart(this._baseURI);
this._started = true;
}
this._parser.parseBuffer(chunk);
} catch (e) {
this.emit('error', e);
} }
cb();
};


inherits(serializer, events.EventEmitter); StreamParser.prototype._flush = function (cb) {

try {
return serializer; this._parser.parseEnd();
} catch (e) {
this.emit('error', e);
}
this.emit('end');
cb();
}; };
17 changes: 7 additions & 10 deletions src/bindings.cc
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010 Norman Heino <norman.heino@gmail.com> * Copyright 2010–2013 Norman Heino <norman.heino@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -14,17 +14,14 @@
* limitations under the License. * limitations under the License.
*/ */


#include <v8.h>
#include <node.h> #include <node.h>


#include "parser.h" #include "parser_wrapper.h"
#include "serializer.h"


using namespace v8; // Node module initializer
using namespace node; void InitModule(v8::Handle<v8::Object> exports) {

ParserWrapper::Initialize(exports);
void Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "newParser", Parser::Initialize);
NODE_SET_METHOD(target, "newSerializer", Serializer::Initialize);
} }


NODE_MODULE(bindings, Initialize); NODE_MODULE(bindings, InitModule)
52 changes: 52 additions & 0 deletions src/message.cc
@@ -0,0 +1,52 @@
/*
* Copyright 2010–2013 Norman Heino <norman.heino@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <sstream>
#include "message.h"

std::string Message::kMessageTypeNone = "";
std::string Message::kMessageTypeDebug = "debug";
std::string Message::kMessageTypeInfo = "info";
std::string Message::kMessageTypeWarning = "warning";
std::string Message::kMessageTypeError = "error";
std::string Message::kMessageTypeFatal = "fatal";

Message::Message(raptor_log_message const* message)
: type_(kMessageTypeNone)
{
switch (message->level) {
case RAPTOR_LOG_LEVEL_NONE:
// already set to none
break;
case RAPTOR_LOG_LEVEL_DEBUG:
type_ = kMessageTypeDebug;
break;
case RAPTOR_LOG_LEVEL_INFO:
type_ = kMessageTypeInfo;
break;
case RAPTOR_LOG_LEVEL_WARN:
type_ = kMessageTypeWarning;
break;
case RAPTOR_LOG_LEVEL_ERROR:
type_ = kMessageTypeError;
break;
case RAPTOR_LOG_LEVEL_FATAL:
type_ = kMessageTypeFatal;
break;
}

text_ = std::string(message->text);
}
37 changes: 37 additions & 0 deletions src/message.h
@@ -0,0 +1,37 @@
/*
* Copyright 2010–2013 Norman Heino <norman.heino@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <raptor.h>

class Message {
public:
explicit Message(raptor_log_message const* message);
std::string const& type() const { return type_; }
std::string const& text() const { return text_; }

private:
static std::string kMessageTypeNone;
static std::string kMessageTypeDebug;
static std::string kMessageTypeInfo;
static std::string kMessageTypeWarning;
static std::string kMessageTypeError;
static std::string kMessageTypeFatal;

std::string& type_;
std::string text_;
};
36 changes: 36 additions & 0 deletions src/namespace.h
@@ -0,0 +1,36 @@
/*
* Copyright 2010–2013 Norman Heino <norman.heino@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <raptor.h>
#include "uri.h"

class Namespace {
public:
explicit Namespace(raptor_namespace const* ns)
: URI_(raptor_namespace_get_uri(ns)),
prefix_(reinterpret_cast<const char*>(raptor_namespace_get_prefix(ns))) {}
std::string URI() const {
return URI_;
}
std::string const& prefix() const {
return prefix_;
}
private:
class URI URI_;
std::string prefix_;
};

0 comments on commit f1e1a02

Please sign in to comment.