summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-01 18:06:04 +0100
committerAndreas Stöckel <andreas@somweyr.de>2015-01-01 18:06:04 +0100
commit4484f6deea0e098a140e878484d020bec839d1bd (patch)
treef3e8b480a533b4158abe9918f16d9988c6b463b6
parent377b7b0213733307927bec9e39c8ccb063e6ba50 (diff)
Using CharReader instead of inputstream in Parser function
-rw-r--r--src/core/parser/Parser.cpp6
-rw-r--r--src/core/parser/Parser.hpp6
-rw-r--r--src/plugins/css/CSSParser.cpp5
-rw-r--r--src/plugins/css/CSSParser.hpp7
-rw-r--r--src/plugins/xml/XmlParser.cpp17
-rw-r--r--src/plugins/xml/XmlParser.hpp4
-rw-r--r--test/plugins/css/CSSParserTest.cpp46
-rw-r--r--test/plugins/xml/XmlParserTest.cpp17
8 files changed, 52 insertions, 56 deletions
diff --git a/src/core/parser/Parser.cpp b/src/core/parser/Parser.cpp
index 23fd9b7..b5d9656 100644
--- a/src/core/parser/Parser.cpp
+++ b/src/core/parser/Parser.cpp
@@ -16,8 +16,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <sstream>
-
#include "Parser.hpp"
namespace ousia {
@@ -25,8 +23,8 @@ namespace parser {
Rooted<Node> Parser::parse(const std::string &str, ParserContext &ctx)
{
- std::istringstream is{str};
- return parse(is, ctx);
+ CharReader reader{str};
+ return parse(reader, ctx);
}
}
}
diff --git a/src/core/parser/Parser.hpp b/src/core/parser/Parser.hpp
index 40ac87a..63303e2 100644
--- a/src/core/parser/Parser.hpp
+++ b/src/core/parser/Parser.hpp
@@ -34,6 +34,7 @@
#include <core/Node.hpp>
#include <core/Registry.hpp>
+#include <core/common/CharReader.hpp>
#include <core/common/Exceptions.hpp>
#include <core/common/Logger.hpp>
@@ -62,6 +63,7 @@ struct ParserContext {
* Reference to the Logger the parser should log any messages to.
*/
Logger &logger;
+
/**
* Reference to the Manager the parser should append nodes to.
*/
@@ -132,7 +134,7 @@ public:
* inclusion in the document graph. This method should be overridden by
* derived classes.
*
- * @param is is a reference to the input stream that should be parsed.
+ * @param reader is a reference to the CharReader that should be used.
* @param ctx is a reference to the context that should be used while
* parsing the document.
* @return a reference to the node representing the subgraph that has been
@@ -140,7 +142,7 @@ public:
* calling code will try to resolve these. If no valid node can be produced,
* a corresponding LoggableException must be thrown by the parser.
*/
- virtual Rooted<Node> parse(std::istream &is, ParserContext &ctx) = 0;
+ virtual Rooted<Node> parse(CharReader &reader, ParserContext &ctx) = 0;
/**
* Parses the given string and returns a corresponding node for
diff --git a/src/plugins/css/CSSParser.cpp b/src/plugins/css/CSSParser.cpp
index 9a041f2..40486cc 100644
--- a/src/plugins/css/CSSParser.cpp
+++ b/src/plugins/css/CSSParser.cpp
@@ -75,10 +75,9 @@ static const std::map<int, CodeTokenDescriptor> CSS_DESCRIPTORS = {
{ESCAPE, {CodeTokenMode::ESCAPE, ESCAPE}},
{LINEBREAK, {CodeTokenMode::LINEBREAK, LINEBREAK}}};
-Rooted<Node> CSSParser::parse(std::istream &is, ParserContext &ctx)
+Rooted<Node> CSSParser::parse(CharReader &reader, ParserContext &ctx)
{
- CharReader input{is};
- CodeTokenizer tokenizer{input, CSS_ROOT, CSS_DESCRIPTORS};
+ CodeTokenizer tokenizer{reader, CSS_ROOT, CSS_DESCRIPTORS};
tokenizer.ignoreComments = true;
tokenizer.ignoreLinebreaks = true;
Rooted<SelectorNode> root = {new SelectorNode{ctx.manager, "root"}};
diff --git a/src/plugins/css/CSSParser.hpp b/src/plugins/css/CSSParser.hpp
index eeb5b2c..6d84dbf 100644
--- a/src/plugins/css/CSSParser.hpp
+++ b/src/plugins/css/CSSParser.hpp
@@ -142,13 +142,16 @@ public:
* rules. You are permitted to just insert a CSS Selector expression
* specifying some part of a DocumentTree you want to refer to.
*
- * @param is is a reference to the input stream that should be parsed.
+ * @param reader is a reference to the CharReader instance from which the
+ * input data should be read.
* @param ctx is a reference to the context that should be used while
* parsing the document.
* @return returns the root node of the resulting SelectorTree. For more
* information on the return conventions consult the Parser.hpp.
*/
- Rooted<Node> parse(std::istream &is, ParserContext &ctx) override;
+ Rooted<Node> parse(CharReader &reader, ParserContext &ctx) override;
+
+ using Parser::parse;
/**
* As befits a class called CSSParser, this Parser parses CSS.
diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp
index 47ed97a..90ea7fa 100644
--- a/src/plugins/xml/XmlParser.cpp
+++ b/src/plugins/xml/XmlParser.cpp
@@ -182,14 +182,13 @@ std::set<std::string> XmlParser::mimetypes()
return std::set<std::string>{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}};
}
-Rooted<Node> XmlParser::parse(std::istream &is, ParserContext &ctx)
+Rooted<Node> XmlParser::parse(CharReader &reader, ParserContext &ctx)
{
// Create the parser object
ScopedExpatXmlParser p{"UTF-8"};
// Create the parser stack instance and pass the reference to the state
// machine descriptor
- CharReader reader(is);
ParserStack stack{ctx, XML_HANDLERS};
XML_SetUserData(&p, &stack);
@@ -207,18 +206,8 @@ Rooted<Node> XmlParser::parse(std::istream &is, ParserContext &ctx)
throw LoggableException{"Internal error: XML parser out of memory!"};
}
- // Read the input file line by line (this needs to be done to allow
- // for nice error messages)
- // TODO: Add a corresponding function to the reader
- size_t bytesRead = 0;
- char *tar = buf;
- while (bytesRead < BUFFER_SIZE && reader.read(*tar)) {
- bytesRead++;
- if (*tar == '\n') {
- break;
- }
- tar++;
- }
+ // Read into the buffer
+ size_t bytesRead = reader.readRaw(buf, BUFFER_SIZE);
// Parse the data and handle any XML error
if (!XML_ParseBuffer(&p, bytesRead, bytesRead == 0)) {
diff --git a/src/plugins/xml/XmlParser.hpp b/src/plugins/xml/XmlParser.hpp
index b19af1e..62f0128 100644
--- a/src/plugins/xml/XmlParser.hpp
+++ b/src/plugins/xml/XmlParser.hpp
@@ -52,11 +52,11 @@ public:
* Parses the given input stream as XML file and returns the parsed
* top-level node.
*
- * @param is is the input stream that will be parsed.
+ * @param reader is the CharReader from which the input should be read.
* @param ctx is a reference to the ParserContext instance that should be
* used.
*/
- Rooted<Node> parse(std::istream &is, ParserContext &ctx) override;
+ Rooted<Node> parse(CharReader &reader, ParserContext &ctx) override;
using Parser::parse;
};
diff --git a/test/plugins/css/CSSParserTest.cpp b/test/plugins/css/CSSParserTest.cpp
index 99e6424..e47d364 100644
--- a/test/plugins/css/CSSParserTest.cpp
+++ b/test/plugins/css/CSSParserTest.cpp
@@ -28,9 +28,8 @@ namespace parser {
namespace css {
TEST(CSSParser, testParseSelectors)
{
- // create a string describing a SelectorTree as input.
- std::stringstream input;
- input << "A>B,A B:r, C#a A[bla=\"blub\"], A::g(4,2,3)";
+ // create a string describing a SelectorTree
+ std::string data{"A>B,A B:r, C#a A[bla=\"blub\"], A::g(4,2,3)"};
/* This should describe the tree:
* root_____
* | \ \
@@ -42,9 +41,9 @@ TEST(CSSParser, testParseSelectors)
// initialize an empty parser context.
StandaloneParserContext ctx;
- // parse the input.
+ // parse the data.
CSSParser instance;
- Rooted<SelectorNode> root = instance.parse(input, ctx).cast<SelectorNode>();
+ Rooted<SelectorNode> root = instance.parse(data, ctx).cast<SelectorNode>();
// we expect three children of the root node overall.
ASSERT_EQ(3, root->getEdges().size());
@@ -153,7 +152,9 @@ TEST(CSSParser, testParseCSS)
// parse the input.
CSSParser instance;
- Rooted<SelectorNode> root = instance.parse(input, ctx).cast<SelectorNode>();
+ CharReader reader{input};
+ Rooted<SelectorNode> root =
+ instance.parse(reader, ctx).cast<SelectorNode>();
// we expect three children of the root node overall.
ASSERT_EQ(3, root->getEdges().size());
@@ -261,26 +262,25 @@ TEST(CSSParser, testParseCSS)
void assertException(std::string css)
{
- std::stringstream input;
- input << css;
- // initialize a parser context.
+ CharReader reader(css);
TerminalLogger logger(std::cerr, true);
- Scope scope(nullptr);
- Registry registry(logger);
- Manager manager;
- ParserContext ctx{scope, registry, logger, manager};
+ {
+ ScopedLogger sl(logger, "test.css", SourceLocation{},
+ CharReader::contextCallback, &reader);
+ Scope scope(nullptr);
+ Registry registry(logger);
+ Manager manager;
+ ParserContext ctx{scope, registry, logger, manager};
- bool seenException = false;
- // parse the input.
- CSSParser instance;
- try {
- instance.parse(input, ctx).cast<SelectorNode>();
- }
- catch (LoggableException ex) {
- logger.log(ex);
- seenException = true;
+ CSSParser instance;
+ try {
+ instance.parse(reader, ctx).cast<SelectorNode>();
+ }
+ catch (LoggableException ex) {
+ logger.log(ex);
+ }
+ ASSERT_TRUE(logger.hasError());
}
- ASSERT_TRUE(seenException);
}
TEST(CSSParser, testParseExceptions)
diff --git a/test/plugins/xml/XmlParserTest.cpp b/test/plugins/xml/XmlParserTest.cpp
index c5a7b10..886cccc 100644
--- a/test/plugins/xml/XmlParserTest.cpp
+++ b/test/plugins/xml/XmlParserTest.cpp
@@ -40,7 +40,7 @@ TEST(XmlParser, mismatchedTagException)
p.parse("<document>\n</document2>", ctx);
}
catch (LoggableException ex) {
- ASSERT_EQ(2U, ex.loc.line);
+ ASSERT_EQ(2, ex.loc.line);
hadException = true;
}
ASSERT_TRUE(hadException);
@@ -66,11 +66,16 @@ TEST(XmlParser, namespaces)
{
StandaloneParserContext ctx(logger);
XmlParser p;
-
- try {
- p.parse(TEST_DATA, ctx);
- } catch(LoggableException ex) {
- logger.log(ex);
+ CharReader reader(TEST_DATA);
+ {
+ ScopedLogger sl(logger, "test.oxd", SourceLocation{},
+ CharReader::contextCallback, &reader);
+ try {
+ p.parse(TEST_DATA, ctx);
+ }
+ catch (LoggableException ex) {
+ logger.log(ex);
+ }
}
}
}