[prettyprint] r591 committed - Add parse_string()....

0 views
Skip to first unread message

prett...@googlecode.com

unread,
Dec 13, 2010, 7:06:53 PM12/13/10
to pp-d...@googlegroups.com
Revision: 591
Author: thockin
Date: Mon Dec 13 16:05:48 2010
Log: Add parse_string().
Make the grammar allow empty input.


http://code.google.com/p/prettyprint/source/detail?r=591

Modified:
/trunk/language/environment.cpp
/trunk/language/environment.h
/trunk/language/grammar.y
/trunk/language/tests/parse_and_validate_test.cpp

=======================================
--- /trunk/language/environment.cpp Fri Jul 9 08:45:37 2010
+++ /trunk/language/environment.cpp Mon Dec 13 16:05:48 2010
@@ -3,6 +3,7 @@
#include <map>
#include "language/language.h"
#include "language/parsed_file.h"
+#include "util/string_file.h"

namespace pp {
namespace language {
@@ -25,11 +26,11 @@
// RAII for file.
boost::shared_ptr<FILE> file_ptr(file, fclose);

- return parse_file(file, name);
+ return parse_file(name, file);
}

const ParsedFile *
-Environment::parse_file(FILE *file, const string &name)
+Environment::parse_file(const string &name, FILE *file)
{
// Parse the file.
std::auto_ptr<ParsedFile> parsed_file_ptr(new ParsedFile());
@@ -47,6 +48,19 @@

return m_parsed_files[name];
}
+
+const ParsedFile *
+Environment::parse_string(const string &name, const string &data)
+{
+ util::StringFile sf;
+
+ if (!sf.open(data)) {
+ WARN("could not create StringFile for data");
+ return NULL;
+ }
+
+ return parse_file(name, sf.get_read_file());
+}

int
Environment::validate(const ValidateOptions &flags)
=======================================
--- /trunk/language/environment.h Fri Jul 9 08:45:37 2010
+++ /trunk/language/environment.h Mon Dec 13 16:05:48 2010
@@ -7,6 +7,7 @@
#include <stdio.h>
#include <ostream>
#include <map>
+#include "language/environment.h"
#include "language/language.h"
#include "util/symbol_table.h"

@@ -92,7 +93,12 @@
// This does not take ownership of the FILE pointer. Returns NULL on
// failure.
const ParsedFile *
- parse_file(FILE *file, const string &name);
+ parse_file(const string &name, FILE *file);
+
+ // Parse a string. This object owns the returned pointer. Returns NULL
+ // on failure.
+ const ParsedFile *
+ parse_string(const string &name, const string &data);

// Validate all parsed files. Returns the number of warnings
// generated.
=======================================
--- /trunk/language/grammar.y Tue Jul 6 21:00:55 2010
+++ /trunk/language/grammar.y Mon Dec 13 16:05:48 2010
@@ -1015,11 +1015,16 @@

// Parsing starts here.
file_scope
+ : file_scope_item_list
+ | { /* nothing */ }
+ ;
+
+file_scope_item_list
: file_scope_item {
SYNTRACE("file_scope", "file_scope_item");
out_parsed_file->add_statement($1);
}
- | file_scope file_scope_item {
+ | file_scope_item_list file_scope_item {
SYNTRACE("file_scope", "file_scope file_scope_item");
out_parsed_file->add_statement($2);
}
=======================================
--- /trunk/language/tests/parse_and_validate_test.cpp Fri Jul 9 08:45:37
2010
+++ /trunk/language/tests/parse_and_validate_test.cpp Mon Dec 13 16:05:48
2010
@@ -38,7 +38,7 @@
parse_and_validate(const string &str)
{
write_to_pipe(str);
- parsed_file = env->parse_file(pipef->get_read_file(), "pipe");
+ parsed_file = env->parse_file("pipe", pipef->get_read_file());
return env->validate(env->default_validate_options()
.set_warn_on_unresolved_symbols());
}

Reply all
Reply to author
Forward
0 new messages