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