Browse Source

Create Org mode parser and lexer

Andrew Young 8 years ago
parent
commit
8a540fadfb
6 changed files with 167 additions and 5 deletions
  1. 5 4
      Makefile.am
  2. 18 1
      configure.ac
  3. 8 0
      src/Makefile.am
  4. 32 0
      src/main.c
  5. 67 0
      src/org_lexer.l
  6. 37 0
      src/org_parser.y

+ 5 - 4
Makefile.am

@@ -1,4 +1,5 @@
-ACLOCAL_AMFLAGS =
-SUBDIRS =
-dist_doc_DATA =
-EXTRA_DIST =
+## Process this file with automake to produce Makefile.in
+ACLOCAL_AMFLAGS = -I m4
+SUBDIRS = src
+dist_doc_DATA = README
+EXTRA_DIST = autogen.sh m4/gnulib-cache.m4

+ 18 - 1
configure.ac

@@ -1,10 +1,27 @@
+# Process this file with autoconf to produce a configure script.
 AC_INIT([org-merge-driver], [0.1], [youngar17@gmail.com])
+AC_CONFIG_AUX_DIR(config)
 AM_INIT_AUTOMAKE([-Wall -Werror gnu])
-AC_PROG_CC
+
+AC_CONFIG_MACRO_DIR([m4])
+
+# Check for programs
+AC_PROG_CC   # c compiler
+AM_PROG_LEX  # flex
+AC_PROG_YACC # bison
+
+# gnulib macros
 gl_EARLY
 gl_INIT
+
+# Check for headers
+# Check for typedefs, structures, and compilers characteristics
+# Check for library functions
+
+# Output files
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([
  Makefile
+ src/Makefile
 ])
 AC_OUTPUT

+ 8 - 0
src/Makefile.am

@@ -0,0 +1,8 @@
+bin_PROGRAMS = org-merge-driver
+
+# Bison and Flex
+BUILT_SOURCES = org_parser.h
+AM_YFLAGS = -d
+AM_LFLAGS = 
+
+org_merge_driver_SOURCES = org_parser.y org_lexer.l main.c

+ 32 - 0
src/main.c

@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <ctype.h>
+/*
+#include "Parser.h"
+#include "Lexer.h"
+*/
+#include "org_mode.tab.h"
+#include "org_lex.yy.h"
+
+char *progname;
+//double yylval;
+
+main( argc, argv )
+char *argv[];
+{
+  progname = argv[0];
+  if (argc > 1)
+    {
+      yyin = fopen (argv[1], "r");
+    }
+  if (yyin == 0)
+    yyin = stdin;
+  //yylex();
+
+  yyparse();
+}
+
+yyerror( s )
+char *s;
+{
+  fprintf( stderr ,"%s: %s\n" , progname , s );
+}

+ 67 - 0
src/org_lexer.l

@@ -0,0 +1,67 @@
+/* Org Mode file lexer -*-c-*- */
+%{
+/* Org Mode lexer */
+#include <stdio.h>
+#include "org_parser.h"
+
+int yywrap();
+
+/* extern int yylex (void); */
+extern void yyerror (char const  *);
+%}
+/*
+%option outfile="org_lex.yy.c" header-file="org_lex.yy.h"
+*/
+
+/* %option reentrant */
+/* %option bison-bridge */
+/* %option bison-locations */
+/* %option yylineno */
+/* %option warn nodefault */
+/* %option noyywrap */
+/* %option bison-bridge */
+
+%x finish_line
+
+%%
+"\n\n\n" { /* New element detection */
+  return NEW_ELEMENT;
+}
+
+"\n" {
+  //printf ("single newline\n");
+  return NEW_LINE;
+}
+
+^[*]+" " { /* Heading Recognition */
+  BEGIN(finish_line);
+  return HEADING;
+}
+
+^"- " {/* Plain List */
+  BEGIN(finish_line);
+  return LIST;
+}
+
+^[0-9]+[).]?" " {/* Numbered List */
+  BEGIN(finish_line);
+  return NUMBERED_LIST;
+}
+
+^. {/* Start of a plain line */
+  BEGIN(finish_line);
+  yymore();
+}
+
+<finish_line>.* {
+  BEGIN(INITIAL);
+  return TEXT;
+}
+%%
+
+int
+yywrap (void)
+{
+  printf ("End of File \n");
+  return 1;
+}

+ 37 - 0
src/org_parser.y

@@ -0,0 +1,37 @@
+/* Org Mode file parser -*-c-*- */
+%{
+#include <stdio.h>
+%}
+
+%defines
+
+%token HEADING TEXT NUMBERED_LIST LIST NEW_LINE NEW_ELEMENT
+
+%%
+org_document: /* empty */
+| org_document heading
+| org_document text
+| org_document numbered_list
+| org_document list
+| org_document NEW_ELEMENT
+;
+
+heading: HEADING TEXT NEW_LINE {printf ("HEADING ");}
+| HEADING NEW_LINE {printf ("heading blank\n");}
+;
+
+numbered_list: NUMBERED_LIST TEXT NEW_LINE {printf ("NUMBERED_LIST ");}
+| NUMBERED_LIST NEW_LINE
+| numbered_list TEXT {printf ("nl_text ");}
+| numbered_list NUMBERED_LIST {printf (" element ");}
+| numbered_list NEW_LINE {printf ("nl_newline\n");}
+;
+
+list: LIST {printf ("new list\n");}
+| list TEXT {printf ("list text\n");}
+| list NEW_LINE
+
+text: TEXT {printf ("new text\n");}
+| text NEW_LINE
+
+%%