You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@corinthia.apache.org by pm...@apache.org on 2015/07/14 13:52:22 UTC
incubator-corinthia git commit: Flat: Print out terms after parsing
Repository: incubator-corinthia
Updated Branches:
refs/heads/master 329722fc1 -> d89f89816
Flat: Print out terms after parsing
Also add command-line options to the main program to parse a given
filename.
Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/d89f8981
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/d89f8981
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/d89f8981
Branch: refs/heads/master
Commit: d89f898165358a4fb3f87138e4039c46fa6d5541
Parents: 329722f
Author: Peter Kelly <pe...@uxproductivity.com>
Authored: Tue Jul 14 18:51:13 2015 +0700
Committer: Peter Kelly <pe...@uxproductivity.com>
Committed: Tue Jul 14 18:51:13 2015 +0700
----------------------------------------------------------------------
experiments/flat/src/Builtin.c | 1 +
experiments/flat/src/CMakeLists.txt | 1 +
experiments/flat/src/Common.h | 26 +++++++++++
experiments/flat/src/Expression.c | 79 ++++++++++++++------------------
experiments/flat/src/Expression.h | 2 +
experiments/flat/src/Grammar.c | 1 +
experiments/flat/src/Parser.c | 1 +
experiments/flat/src/Term.c | 45 ++++++++++++++++++
experiments/flat/src/Term.h | 2 +
experiments/flat/src/Util.c | 46 +++++++++++++++++++
experiments/flat/src/Util.h | 2 +
experiments/flat/src/flat.c | 63 +++++++++++++++++++++++--
12 files changed, 219 insertions(+), 50 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Builtin.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Builtin.c b/experiments/flat/src/Builtin.c
index 9b3c32e..ee8fb81 100644
--- a/experiments/flat/src/Builtin.c
+++ b/experiments/flat/src/Builtin.c
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+#include "Common.h"
#include "Builtin.h"
#include <stdarg.h>
#include <stdlib.h>
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/experiments/flat/src/CMakeLists.txt b/experiments/flat/src/CMakeLists.txt
index eb969c1..e7287d3 100644
--- a/experiments/flat/src/CMakeLists.txt
+++ b/experiments/flat/src/CMakeLists.txt
@@ -25,6 +25,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(SOURCES
Builtin.c
Builtin.h
+ Common.h
Expression.c
Expression.h
Grammar.c
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Common.h
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Common.h b/experiments/flat/src/Common.h
new file mode 100644
index 0000000..90c8f91
--- /dev/null
+++ b/experiments/flat/src/Common.h
@@ -0,0 +1,26 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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
+
+// Ultimately we should use DFPlatform.h or a similarly-named shared header here. However currently
+// I want to keep the source tree for Flat separate from DocFormats so it can work as an independent
+// library, at least during the "experimental" phase of development.
+
+#ifdef _WINDOWS
+#define snprintf _snprintf
+#endif // _WINDOWS
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Expression.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Expression.c b/experiments/flat/src/Expression.c
index c4d30d5..3a9873d 100644
--- a/experiments/flat/src/Expression.c
+++ b/experiments/flat/src/Expression.c
@@ -15,7 +15,9 @@
// specific language governing permissions and limitations
// under the License.
+#include "Common.h"
#include "Expression.h"
+#include "Util.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@@ -30,6 +32,38 @@ struct Expression {
Expression *children[];
};
+const char *ExprKindAsString(ExprKind kind)
+{
+ switch (kind) {
+ case ChoiceExpr:
+ return "Choice";
+ case SequenceExpr:
+ return "Sequence";
+ case AndExpr:
+ return "And";
+ case NotExpr:
+ return "Not";
+ case OptExpr:
+ return "Opt";
+ case StarExpr:
+ return "Star";
+ case PlusExpr:
+ return "Plus";
+ case IdentExpr:
+ return "Ident";
+ case LitExpr:
+ return "Lit";
+ case ClassExpr:
+ return "Class";
+ case DotExpr:
+ return "Dot";
+ case RangeExpr:
+ return "Range";
+ default:
+ return "?";
+ }
+}
+
Expression *ExpressionNewChoice(int count, Expression **children)
{
for (int i = 0; i < count; i++)
@@ -192,51 +226,6 @@ static int ExprKindPrecedence(ExprKind kind)
return 0;
}
-static void printEscapedRangeChar(char c)
-{
- switch (c) {
- case '[':
- printf("\\[");
- break;
- case ']':
- printf("\\]");
- break;
- case '\\':
- printf("\\\\");
- break;
- default:
- printf("%c",c);
- break;
- }
-}
-
-static void printLiteral(const char *value)
-{
- printf("'");
- for (int i = 0; value[i] != '\0'; i++) {
- switch (value[i]) {
- case '\r':
- printf("\\r");
- break;
- case '\n':
- printf("\\n");
- break;
- case '\t':
- printf("\\t");
- break;
- case '\'':
- printf("\\'");
- break;
- case '\\':
- printf("\\\\");
- break;
- default:
- printf("%c",value[i]);
- }
- }
- printf("'");
-}
-
void ExpressionPrint(Expression *expr, int highestPrecedence, const char *indent)
{
int exprPrecedence = ExprKindPrecedence(expr->kind);
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Expression.h
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Expression.h b/experiments/flat/src/Expression.h
index e38614e..3acb875 100644
--- a/experiments/flat/src/Expression.h
+++ b/experiments/flat/src/Expression.h
@@ -88,6 +88,8 @@ typedef enum {
typedef struct Expression Expression;
+const char *ExprKindAsString(ExprKind kind);
+
Expression *ExpressionNewChoice(int count, Expression **children);
Expression *ExpressionNewSequence(int count, Expression **children);
Expression *ExpressionNewAnd(Expression *child);
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Grammar.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Grammar.c b/experiments/flat/src/Grammar.c
index 8201b89..4f71f16 100644
--- a/experiments/flat/src/Grammar.c
+++ b/experiments/flat/src/Grammar.c
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+#include "Common.h"
#include "Grammar.h"
#include <stdlib.h>
#include <string.h>
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Parser.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Parser.c b/experiments/flat/src/Parser.c
index 2fb7b13..0425b33 100644
--- a/experiments/flat/src/Parser.c
+++ b/experiments/flat/src/Parser.c
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+#include "Common.h"
#include "Parser.h"
#include "Util.h"
#include <stdlib.h>
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Term.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Term.c b/experiments/flat/src/Term.c
index 735d609..902dfd7 100644
--- a/experiments/flat/src/Term.c
+++ b/experiments/flat/src/Term.c
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+#include "Common.h"
#include "Term.h"
#include "Util.h"
#include <stdlib.h>
@@ -50,3 +51,47 @@ void TermListPtrAppend(TermList ***listPtr, Term *term)
**listPtr = TermListNew(term,NULL);
*listPtr = &(**listPtr)->next;
}
+
+void TermPrint(Term *term, const char *input, int indent)
+{
+ for (int i = 0; i < indent; i++)
+ printf(" ");
+
+ switch (ExpressionKind(term->type)) {
+ case IdentExpr:
+ printf("%s %s\n",ExprKindAsString(ExpressionKind(term->type)),ExprIdentValue(term->type));
+ break;
+ case LitExpr:
+ case RangeExpr:
+ case DotExpr: {
+ int start = term->start;
+ int end = term->end;
+ int inputLen = strlen(input);
+ if ((start >= 0) && (start <= inputLen) && (end >= 0) && (end <= inputLen) && (start <= end)) {
+ char *temp = (char*)malloc(end-start+1);
+ memcpy(temp,&input[start],end-start);
+ temp[end-start] = '\0';
+
+ printf("%s ",ExprKindAsString(ExpressionKind(term->type)));
+ printLiteral(temp);
+ printf("\n");
+
+ free(temp);
+ }
+ else {
+ printf("%s\n",ExprKindAsString(ExpressionKind(term->type)));
+ }
+ break;
+ }
+ default:
+ printf("%s\n",ExprKindAsString(ExpressionKind(term->type)));
+ break;
+ }
+
+ for (TermList *child = term->children; child != NULL; child = child->next) {
+ if (child->next != NULL)
+ TermPrint(child->term,input,indent+1);
+ else
+ TermPrint(child->term,input,indent+1);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Term.h
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Term.h b/experiments/flat/src/Term.h
index 4b444c5..911396a 100644
--- a/experiments/flat/src/Term.h
+++ b/experiments/flat/src/Term.h
@@ -38,3 +38,5 @@ Term *TermNew(Expression *type, int start, int end, TermList *children);
TermList *TermListNew(Term *term, TermList *next);
void TermListPtrAppend(TermList ***listPtr, Term *term);
+
+void TermPrint(Term *term, const char *input, int indent);
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Util.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Util.c b/experiments/flat/src/Util.c
index e8960a4..e03e8e0 100644
--- a/experiments/flat/src/Util.c
+++ b/experiments/flat/src/Util.c
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+#include "Common.h"
#include "Util.h"
#include <stdio.h>
@@ -75,3 +76,48 @@ uint32_t UTF8NextChar(const char *str, size_t *offsetp)
return 0;
}
}
+
+void printEscapedRangeChar(char c)
+{
+ switch (c) {
+ case '[':
+ printf("\\[");
+ break;
+ case ']':
+ printf("\\]");
+ break;
+ case '\\':
+ printf("\\\\");
+ break;
+ default:
+ printf("%c",c);
+ break;
+ }
+}
+
+void printLiteral(const char *value)
+{
+ printf("'");
+ for (int i = 0; value[i] != '\0'; i++) {
+ switch (value[i]) {
+ case '\r':
+ printf("\\r");
+ break;
+ case '\n':
+ printf("\\n");
+ break;
+ case '\t':
+ printf("\\t");
+ break;
+ case '\'':
+ printf("\\'");
+ break;
+ case '\\':
+ printf("\\\\");
+ break;
+ default:
+ printf("%c",value[i]);
+ }
+ }
+ printf("'");
+}
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/Util.h
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Util.h b/experiments/flat/src/Util.h
index eb46072..2fc9c6b 100644
--- a/experiments/flat/src/Util.h
+++ b/experiments/flat/src/Util.h
@@ -21,3 +21,5 @@
#include <stdint.h>
uint32_t UTF8NextChar(const char *str, size_t *offsetp);
+void printEscapedRangeChar(char c);
+void printLiteral(const char *value);
http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d89f8981/experiments/flat/src/flat.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/flat.c b/experiments/flat/src/flat.c
index e34172c..51003b8 100644
--- a/experiments/flat/src/flat.c
+++ b/experiments/flat/src/flat.c
@@ -15,14 +15,67 @@
// specific language governing permissions and limitations
// under the License.
-#include <stdio.h>
+#include "Common.h"
#include "Builtin.h"
+#include "Parser.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define READ_SIZE 1024
+
+static char *readStringFromFile(const char *filename)
+{
+ FILE *f = fopen(filename,"rb");
+ if (f == NULL) {
+ perror(filename);
+ return NULL;
+ }
+
+ char *data = (char *)malloc(READ_SIZE);
+ size_t len = 0;
+ size_t r;
+ while (0 < (r = fread(&data[len],1,READ_SIZE,f))) {
+ len += r;
+ data = (char*)realloc(data,len+READ_SIZE);
+ }
+ data = (char*)realloc(data,len+1);
+ data[len] = '\0';
+ fclose(f);
+ return data;
+}
int main(int argc, const char **argv)
{
- // Build and print out the built-in PEG grammar
- Grammar *gram = GrammarNewBuiltin();
- GrammarPrint(gram);
- GrammarFree(gram);
+
+ if ((argc == 2) && !strcmp(argv[1],"-g")) {
+ // Build and print out the built-in PEG grammar
+ Grammar *gram = GrammarNewBuiltin();
+ GrammarPrint(gram);
+ GrammarFree(gram);
+ }
+ else if ((argc == 3) && !strcmp(argv[1],"-p")) {
+ const char *filename = argv[2];
+ char *input = readStringFromFile(filename);
+ if (input == NULL) {
+ perror(filename);
+ exit(1);
+ }
+ Grammar *gram = GrammarNewBuiltin();
+ Term *term = parse(gram,"Grammar",input,0,strlen(input));
+ printf("Parsed term %p\n",term);
+ TermPrint(term,input,0);
+ free(input);
+ GrammarFree(gram);
+ }
+ else {
+ printf("Usage:\n"
+ "\n"
+ "flat -g Print built-in PEG grammar\n"
+ "\n"
+ "flat -p FILENAME Parse FILENAME using the built-in PEG grammar, and print\n"
+ " the resulting parse tree\n");
+ return 1;
+ }
return 0;
}