1 /*
2    File: dcg_parser2.c
3    Parses all files indicated by the argument string
4 
5    Copyright (C) 2008 Marc Seutter
6 
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 
20    CVS ID: "$Id: dcg_parser2.c,v 1.10 2008/06/28 13:03:45 marcs Exp $"
21 */
22 
23 /* standard includes */
24 #include <stdio.h>
25 #include <string.h>
26 
27 /* Conditional inclusion to define MAXPATHLEN */
28 #ifndef WIN32
29 #include <sys/param.h>
30 #endif
31 #ifndef MAXPATHLEN
32 #define MAXPATHLEN 256
33 #endif
34 
35 /* libdcg includes */
36 #include <dcg.h>
37 #include <dcg_error.h>
38 
39 /* local includes */
40 #include "dcg_code.h"
41 #include "dcg_lex.h"
42 #include "dcg_parser2.h"
43 
try_open(char * basename)44 static FILE *try_open (char *basename)
45 	{ FILE *new;
46 	  char fname[MAXPATHLEN + 1];
47 	  sprintf (fname, "%s.dcg", basename);
48 	  if ((new = fopen (fname, "r")))
49 	     hint ("parsing %s...", fname);
50 	  else
51 	     { sprintf (fname, "%s", basename);
52 	       if ((new = fopen (fname, "r")))
53 		  hint ("parsing %s...", fname);
54 	       else panic ("could not open %s", fname);
55 	     };
56 	  return (new);
57 	};
58 
parse_imported_definitions(string iname,string_list include_path)59 static void parse_imported_definitions (string iname, string_list include_path)
60 	{ FILE *new;
61 	  char fname[MAXPATHLEN + 1];
62 	  int ix;
63 	  for (ix = 0; ix < include_path -> size; ix++)
64 	     { sprintf (fname, "%s/%s.def", include_path -> array[ix], iname);
65 	       if ((new = fopen (fname, "r")))
66 	          { hint ("parsing definitions from %s.def...", iname);
67 		    init_lex (new);
68 		    pdcg_parse ();
69 		    fclose (new);
70 		    return;
71 		  };
72 	     };
73 	  panic ("could not locate %s[.def]", iname);
74 	};
75 
76 #define STDDEF "dcg"
parse(char * basename,string_list include_path)77 void parse (char *basename, string_list include_path)
78 	{ FILE *f = try_open (basename);
79 	  int ix;
80 	  imp_types = new_type_list ();
81 	  all_defs = new_def_list ();
82 	  all_stats = new_stat_list ();
83 	  init_lex (f);
84 	  pdcg_parse ();
85 	  fclose (f);
86 	  for (ix = 0; ix < all_stats -> size; ix++)
87 	     { stat s = all_stats -> array[ix];
88 	       if (s -> tag == TAGImport)
89 	          parse_imported_definitions (s -> Import.imp, include_path);
90 	     };
91 	  parse_imported_definitions (STDDEF, include_path);
92 	};
93