import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import coboled.editors.CobolElement.CobolElementType;
-
public class CobolParser {
public CobolElement parse(String source) {
- CobolElement root = new CobolElement("root", CobolElementType.OTHER);
+ CobolElement root = new CobolElement("root", CobolElementType.ROOT);
try {
BufferedReader reader = new BufferedReader(new StringReader(source));
int offset = 0;
String line = null;
+ CobolElement work = root;
while ((line = reader.readLine()) != null) {
-
// 部の記述があった場合
- Pattern pattern = Pattern.compile("^.{6}\\s(\\s{0,3}[^\\s].*?DIVISION)\\.",
- Pattern.CASE_INSENSITIVE);
+ Pattern pattern = Pattern.compile("^.{6}\\s\\s{0,3}([^\\s]*)\\s+(DIVISION)\\.", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
boolean match = matcher.matches();
if (match) {
- CobolElement divisionElement = new CobolElement(matcher
- .group(1).trim(), CobolElementType.DIVISION);
- divisionElement.setParent(root);
- divisionElement.setOffset(offset);
- root.appendChild(divisionElement);
+ String name = matcher.group(1) + " " + matcher.group(2);
+ CobolElement element = new CobolElement(name, CobolElementType.DIVISION);
+ CobolElement parent = getParentElement(work, element);
+ element.setParent(parent);
+ element.setOffset(offset);
+ root.appendChild(element);
+ work = element;
offset = offset + line.length() + 1;
continue;
}
// 節の記述があった場合
- Pattern pattern2 = Pattern.compile("^.{6}\\s(\\s{0,3}[^\\s].*?SECTION)\\.",
- Pattern.CASE_INSENSITIVE);
+ Pattern pattern2 = Pattern.compile("^.{6}\\s\\s{0,3}([^\\s]*)\\s+(SECTION)\\.", Pattern.CASE_INSENSITIVE);
Matcher matcher2 = pattern2.matcher(line);
boolean match2 = matcher2.matches();
if (match2) {
- CobolElement sectionElement = new CobolElement(matcher2
- .group(1).trim(), CobolElementType.SECTION);
- CobolElement parent = getLastElement(root);
- sectionElement.setParent(parent);
- sectionElement.setOffset(offset);
- parent.appendChild(sectionElement);
+ String name = matcher2.group(1) + " " + matcher2.group(2);
+ CobolElement element = new CobolElement(name, CobolElementType.SECTION);
+ CobolElement parent = getParentElement(work, element);
+ element.setParent(parent);
+ parent.appendChild(element);
+ element.setOffset(offset);
+ work = element;
offset = offset + line.length() + 1;
continue;
}
-
- // その他
- Pattern pattern3 = Pattern.compile("^.{6}\\s(\\s{0,3}[^\\s].*)",
- Pattern.CASE_INSENSITIVE);
+
+ // データ定義
+ Pattern pattern4 = Pattern.compile("^.{6}\\s\\s{0,3}([0-9][0-9]\\s+[^\\s]*)(.*)", Pattern.CASE_INSENSITIVE);
+ Matcher matcher4 = pattern4.matcher(line);
+ boolean match4 = matcher4.matches();
+ if (match4) {
+ String name = matcher4.group(1);
+ CobolElement element = new CobolElement(name, CobolElementType.DATA);
+ CobolElement parent = getParentElement(work, element);
+ element.setParent(parent);
+ element.setOffset(offset);
+ parent.appendChild(element);
+ work = element;
+ offset = offset + line.length() + 1;
+ continue;
+ }
+
+ // 段落
+ Pattern pattern3 = Pattern.compile("^.{6}\\s\\s{0,3}([^\\s]+)\\s*(.*)", Pattern.CASE_INSENSITIVE);
Matcher matcher3 = pattern3.matcher(line);
boolean match3 = matcher3.matches();
if (match3) {
- CobolElement sectionElement = new CobolElement(matcher3
- .group(1), CobolElementType.OTHER);
- CobolElement parent = getLastElement(root);
- sectionElement.setParent(parent);
- sectionElement.setOffset(offset);
- parent.appendChild(sectionElement);
- offset = offset + line.length() + 1;
- continue;
+ String name = matcher3.group(1);
+ if(name.indexOf("-EXIT") == -1) {
+ CobolElement element = new CobolElement(name, CobolElementType.PARAGRAPH);
+ CobolElement parent = getParentElement(work, element);
+ element.setParent(parent);
+ element.setOffset(offset);
+ parent.appendChild(element);
+ work = element;
+ offset = offset + line.length() + 1;
+ continue;
+ }
}
-
+
offset = offset + line.length() + 1;
-
}
} catch (IOException e) {
// TODO 自動生成された catch ブロック
}
return root;
}
-
- private static CobolElement getLastElement(CobolElement element) {
- if (element.getChildren() != null && element.getChildren().length != 0) {
- int lastIndex = element.getChildren().length - 1;
- return element.getChildren()[lastIndex];
+
+ private CobolElement getParentElement(CobolElement element, CobolElement element2) {
+ CobolElementType type = element.getType();
+ CobolElementType type2 = element2.getType();
+ if(type.compareTo(type2) > 0) {
+ return element;
+ } else if(type.compareTo(type2) == 0) {
+ return element.getParent();
+ } else {
+ return getParentElement(element.getParent(), element2);
}
- return element;
}
}