1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package io.github.prolobjectlink.prolog.jpl7;
30
31 import static io.github.prolobjectlink.prolog.PrologLogger.FILE_NOT_FOUND;
32 import static io.github.prolobjectlink.prolog.PrologLogger.IO;
33
34 import java.io.BufferedReader;
35 import java.io.File;
36 import java.io.FileNotFoundException;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.io.Reader;
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import org.jpl7.Atom;
44 import org.jpl7.Compound;
45 import org.jpl7.Term;
46 import org.jpl7.Util;
47
48
49
50
51
52
53 final class JplParser {
54
55 public Term parseTerm(String term) {
56 return Util.textToTerm(term);
57 }
58
59 public Term[] parseTerms(Term term) {
60 return parseTerms("" + term + "");
61 }
62
63 public Term[] parseTerms(String stringTerms) {
64 Term[] a = new Term[0];
65 Term ptr = Util.textToTerm(stringTerms);
66 List<Term> terms = new ArrayList<Term>();
67 while (ptr.isCompound() && ptr.hasFunctor(",", 2)) {
68 terms.add(ptr.arg(1));
69 ptr = ptr.arg(2);
70 }
71 terms.add(ptr);
72 return terms.toArray(a);
73 }
74
75 public JplProgram parseProgram(String file) {
76 return parseProgram(new File(file));
77 }
78
79 public JplProgram parseProgram(File in) {
80
81 FileReader reader = null;
82 BufferedReader buffer = null;
83 JplProgram program = new JplProgram();
84
85 try {
86 reader = new FileReader(in);
87 buffer = new BufferedReader(reader);
88 String line = buffer.readLine();
89 StringBuilder b = new StringBuilder();
90 while (line != null) {
91 if (!line.isEmpty() && line.lastIndexOf('.') == line.length() - 1) {
92 b.append(line.substring(0, line.length() - 1));
93 Term clauseTerm = Util.textToTerm("" + b + "");
94 if (clauseTerm.hasFunctor(":-", 1)) {
95 String absoluteString = "";
96 Term arg = clauseTerm.arg(1);
97 if (arg.hasFunctor("consult", 1)) {
98 Term relative = arg.arg(1);
99 String path = relative.name();
100 String[] array = path.split("\\.\\./");
101 if (array.length > 1) {
102 String ok = array[array.length - 1];
103 File currentPtr = in.getCanonicalFile();
104 for (int i = 0; i < array.length; i++) {
105 currentPtr = currentPtr.getParentFile();
106 }
107 String ptr = currentPtr.getCanonicalPath();
108 File abs = new File(ptr + File.separator + ok);
109 absoluteString = abs.getCanonicalPath();
110 }
111 Atom absolute = new Atom(absoluteString.toLowerCase().replace(File.separatorChar, '/'));
112 Compound c = new Compound("consult", new Term[] { absolute });
113 program.addDirective(c);
114 } else {
115 program.addDirective(clauseTerm);
116 }
117
118 } else {
119 program.add(clauseTerm);
120 }
121 b = new StringBuilder();
122 } else {
123 b.append(line);
124 }
125 line = buffer.readLine();
126 }
127 } catch (FileNotFoundException e) {
128 JplProvider.logger.error(getClass(), FILE_NOT_FOUND, e);
129 } catch (IOException e) {
130 JplProvider.logger.error(getClass(), IO, e);
131 } finally {
132 if (reader != null) {
133 try {
134 reader.close();
135 } catch (IOException e) {
136 JplProvider.logger.error(getClass(), IO, e);
137 }
138 }
139 if (buffer != null) {
140 try {
141 buffer.close();
142 } catch (IOException e) {
143 JplProvider.logger.error(getClass(), IO, e);
144 }
145 }
146 }
147
148 return program;
149 }
150
151 public JplProgram parseProgram(Reader in) {
152
153 BufferedReader buffer = null;
154 JplProgram program = new JplProgram();
155
156 try {
157 buffer = new BufferedReader(in);
158 String line = buffer.readLine();
159 StringBuilder b = new StringBuilder();
160 while (line != null) {
161 if (!line.isEmpty() && line.lastIndexOf('.') == line.length() - 1) {
162 b.append(line.substring(0, line.length() - 1));
163 Term clauseTerm = Util.textToTerm("" + b + "");
164 program.add(clauseTerm);
165 b = new StringBuilder();
166 } else {
167 b.append(line);
168 }
169 line = buffer.readLine();
170 }
171 } catch (IOException e) {
172 JplProvider.logger.error(getClass(), IO, e);
173 } finally {
174 if (buffer != null) {
175 try {
176 buffer.close();
177 } catch (IOException e) {
178 JplProvider.logger.error(getClass(), IO, e);
179 }
180 }
181 }
182
183 return program;
184 }
185
186 }