use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.
the class SLNParser method parseMacroAtomDictionaryObject.
public ISLNDictionaryObject parseMacroAtomDictionaryObject(String dictObjectString, boolean parseName) {
if (dictObjectString.isEmpty())
return null;
int pos = 0;
int n = dictObjectString.length();
String dictObjName = null;
if (parseName) {
if (Character.isUpperCase(dictObjectString.charAt(0)))
pos++;
else {
newError("Incorrect dictionary object: " + dictObjectString, pos, "");
return null;
}
while ((pos < n) && (Character.isLowerCase(dictObjectString.charAt(pos)) || Character.isDigit(dictObjectString.charAt(pos)) || dictObjectString.charAt(pos) == '_')) {
pos++;
}
if (pos == n) {
newError("Incorrecr dictionary object: " + dictObjectString, -1, "");
return null;
}
dictObjName = dictObjectString.substring(0, pos);
if (dictObjectString.charAt(pos) != ':') {
newError("Incorrecr dictionary object: " + dictObjectString, -1, "");
return null;
} else
pos++;
if (pos == n) {
newError("Incorrect dictionary object: " + dictObjectString, -1, "");
return null;
}
}
ParserState state = getState();
errorContextPrefix = "Parsing macro/markush atom: " + dictObjectString + ": ";
// Parsing a macro atom
sln = dictObjectString.substring(pos);
container = new SLNContainer(SilentChemObjectBuilder.getInstance());
init();
parse();
ISLNDictionaryObject dictObj = null;
if (errors.isEmpty()) {
if (FlagUseSimpleMacroAtomsInDictionary && container.getAtomCount() == 1)
dictObj = new AtomDictionaryObject(dictObjName, dictObjectString, container);
else
dictObj = new MacroAtomDictionaryObject(dictObjName, dictObjectString, container);
}
restoreState(state);
errorContextPrefix = "";
return dictObj;
}
use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.
the class SLNParser method parseLocalDictionaryObjects.
SLNDictionary parseLocalDictionaryObjects() {
if (!errors.isEmpty())
return null;
if (localDictionaryObjectBeginPos.isEmpty())
return null;
SLNDictionary dict = new SLNDictionary();
for (int i = 0; i < localDictionaryObjectBeginPos.size(); i++) {
int beginPos = localDictionaryObjectBeginPos.get(i) + 1;
int endPos = localDictionaryObjectEndPos.get(i);
String locDictObjStr = sln.substring(beginPos, endPos);
// System.out.println(locDictObjStr);
ISLNDictionaryObject dictObj = parseDictionaryObject(locDictObjStr);
if (dictObj != null)
dict.addDictionaryObject(dictObj);
}
dict.checkDictionary();
if (!dict.getCheckErrors().isEmpty()) {
for (int i = 0; i < dict.getCheckErrors().size(); i++) newError(dict.getCheckErrors().get(i), 0, "");
}
return dict;
}
use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.
the class SLNDictionary method getDictionary.
public static SLNDictionary getDictionary(String[] dictionaryObjects, SLNParser parser) {
if (dictionaryObjects == null)
return null;
if (parser == null)
return null;
parser.getErrors().clear();
SLNDictionary dict = new SLNDictionary();
for (int i = 0; i < dictionaryObjects.length; i++) {
String s = dictionaryObjects[i];
if (s.isEmpty() || !s.startsWith("{") || !s.endsWith("}")) {
parser.getErrors().add(new SLNParserError(s, "Missing open or close brackets {}", 0, ""));
continue;
}
String dictObjStr = s.substring(1, s.length() - 1);
ISLNDictionaryObject dictObj = parser.parseDictionaryObject(dictObjStr);
if (dictObj != null)
dict.addDictionaryObject(dictObj);
}
if (!parser.getErrors().isEmpty()) {
// Store errors inside the dictionary
dict.parserErrors.addAll(parser.getErrors());
parser.getErrors().clear();
}
return dict;
}
use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.
the class SLNDictionary method getDictionary.
public static SLNDictionary getDictionary(String fileName, SLNParser parser) throws Exception {
if (fileName == null)
return null;
if (parser == null)
return null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
throw e;
}
parser.getErrors().clear();
SLNDictionary dict = new SLNDictionary();
try {
int nLine = 0;
String line;
while ((line = br.readLine()) != null) {
nLine++;
String s = line.trim();
if (s.isEmpty()) {
// System.out.println("Empty SLN string in line #" + nLine);
continue;
}
if (s.isEmpty() || !s.startsWith("{") || !s.endsWith("}")) {
parser.getErrors().add(new SLNParserError("Line #" + nLine + " " + s, "Missing open or close brackets {}", 0, ""));
continue;
}
String dictObjStr = s.substring(1, s.length() - 1);
ISLNDictionaryObject dictObj = parser.parseDictionaryObject(dictObjStr);
if (dictObj != null)
dict.addDictionaryObject(dictObj);
}
br.close();
} catch (IOException e) {
throw e;
}
if (!parser.getErrors().isEmpty()) {
// Store errors inside the dictionary
dict.parserErrors.addAll(parser.getErrors());
parser.getErrors().clear();
}
return dict;
}
use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.
the class Expander method expandDictionaryAtom.
public List<IAtom> expandDictionaryAtom(SLNAtom at) {
// Handle a dictionary object
if (at.dictObj instanceof AtomDictionaryObject) {
AtomDictionaryObject aDO = (AtomDictionaryObject) at.dictObj;
SLNAtom newAt = aDO.atom.clone();
expContainer.addAtom(newAt);
List<IAtom> list = new ArrayList<IAtom>();
list.add(newAt);
return list;
} else if (at.dictObj instanceof MacroAtomDictionaryObject) {
return expandMacroAtomDictionaryObject((MacroAtomDictionaryObject) at.dictObj);
} else if (at.dictObj instanceof MarkushAtomDictionaryObject) {
// Get the proper macro/atom component from the Markush atom
// according to the markushPos[...] value
// maObj is either AtomDictionaryObject or MacroAtomDictionaryObject
ISLNDictionaryObject maObj = getProperDictionaryObject(at);
if (maObj instanceof AtomDictionaryObject) {
AtomDictionaryObject aDO = (AtomDictionaryObject) maObj;
SLNAtom newAt = aDO.atom.clone();
expContainer.addAtom(newAt);
List<IAtom> list = new ArrayList<IAtom>();
list.add(newAt);
return list;
} else {
return expandMacroAtomDictionaryObject((MacroAtomDictionaryObject) maObj);
}
}
return null;
}
Aggregations