bool CFParser :: applyRule(size_t ruleId, TokenInfo& token, _ScriptReader& reader) { ident_t name = retrieveKey(_names.start(), ruleId, DEFAULT_STR); size_t readerRollback = reader.Position(); size_t coderRollback = token.LogPosition(); RuleMap::Iterator it = _rules.getIt(ruleId); while(!it.Eof()) { CFParser::TokenInfo tokenCopy(token); if ((*it).apply(*it, tokenCopy, reader)) { tokenCopy.save(token); return true; } // roll back reader.seek(readerRollback); token.trimLog(coderRollback); it = _rules.getNextIt(ruleId, it); } return false; }
void TreeScriptParser :: parseScope(_ScriptReader& reader, ScriptBookmark& bm, SyntaxWriter& writer) { bm = reader.read(); while (!reader.Eof() && !reader.compare(")")) { parseStatement(reader, bm, writer); bm = reader.read(); } }
bool VMTapeParser :: parse(TapeWriter& writer, _ScriptReader& reader) { bool idle = true; ScriptBookmark bm = reader.read(); while (!reader.Eof()) { idle = false; parseStatement(reader, bm, writer); bm = reader.read(); } return !idle; }
bool CFParser :: parseGrammarMode(_ScriptReader& reader) { ScriptBookmark bm = reader.read(); while (!reader.compare(";")) { if (reader.compare("symbolic")) { _symbolMode = true; } // else throw EParseError(reader.info.column, reader.info.row); bm = reader.read(); } return true; }
int CFParser :: buildDerivationTree(_ScriptReader& reader, size_t startRuleId, MemoryWriter& writer) { DerivationQueue predictions; predictions.push(DerivationItem(startRuleId, 0, -1)); ScriptBookmark bm; while (predictions.Count() > 0) { predictions.push(DerivationItem(0)); bm = reader.read(); int terminalOffset = writer.Position(); writer.write(&bm, sizeof(ScriptBookmark)); DerivationItem current = predictions.pop(); while (current.ruleId != 0) { if (current.ruleId == -1) { return current.trace; } predict(predictions, current, reader, bm, terminalOffset, writer); current = predictions.pop(); } } throw EParseError(bm.column, bm.row); }
void CFParser :: parse(_ScriptReader& reader, TapeWriter& writer) { // if (_symbolMode) // reader.switchDFA(dfaSymbolic); ScriptLog log; TokenInfo token(this, &log); token.read(reader); size_t readerRollback = reader.Position(); //size_t outputRollback = compiler->Position(); size_t startId = mapRuleId("start"); if (token.state != dfaEOF) { if (!applyRule(startId, token, reader)) throw EParseError(token.column, token.row); if (token.state != dfaEOF) throw EParseError(token.column, token.row); } IdentifierTextReader logReader((ident_t)log.getBody()); CachedScriptReader scriptReader(&logReader); _baseParser->parse(scriptReader, writer); }
void saveReference(_ScriptReader& scriptReader, CFParser* parser, ref_t ptr, ScriptLog& log) { ScriptBookmark bm; parser->readScriptBookmark(ptr, bm); log.write(scriptReader.lookup(bm)); }
bool CFParser :: compareToken(_ScriptReader& reader, ScriptBookmark& bm, int rule) { ident_t terminal = reader.lookup(bm); ident_t ruleTerminal = getBodyText(rule); return terminal.compare(ruleTerminal); }
bool CFParser :: parseGrammarRule(_ScriptReader& reader) { ScriptBookmark bm = reader.read(); if (bm.state != dfaIdentifier) throw EParseError(bm.column, bm.row); size_t ruleId = mapRuleId(reader.lookup(bm)); reader.read(); if (!reader.compare("::=")) throw EParseError(bm.column, bm.row); bm = reader.read(); Rule rule; defineGrammarRule(reader, bm, rule, ruleId); addRule(ruleId, rule); return true; }
void TreeScriptParser :: parse(_ScriptReader& reader, MemoryDump* output) { MemoryWriter writer(output); writer.writeDWord(0); // HOTFIX : end of tape int sizePos = writer.Position(); writer.writeDWord(0); SyntaxTree tree; SyntaxWriter treeWriter(tree); ScriptBookmark bm = reader.read(); while (!reader.Eof()) { parseStatement(reader, bm, treeWriter); bm = reader.read(); } if (tree.save(output)) { writer.seek(sizePos); writer.writeDWord(output->Length() - sizePos); } else output->clear(); }
void CFParser :: parse(_ScriptReader& reader, MemoryDump* output) { if (_symbolMode) reader.switchDFA(dfaSymbolic); ScriptLog log; size_t startId = mapRuleId("start"); while (!reader.Eof()) { MemoryWriter writer(&_body); int presaved = writer.Position(); int trace = buildDerivationTree(reader, startId, writer); generateOutput(trace, reader, log); IdentifierTextReader logReader((const char*)log.getBody()); ScriptReader scriptReader(&logReader); _baseParser->parse(scriptReader, output); _body.trim(presaved); log.clear(); } }
void TreeScriptParser :: parseStatement(_ScriptReader& reader, ScriptBookmark& bm, SyntaxWriter& writer) { int type = _tokens.get(reader.lookup(bm)); if (type != 0) { writer.newNode((LexicalType)type); bm = reader.read(); if (reader.compare("(")) { parseScope(reader, bm, writer); } else if (reader.compare("=")) { bm = reader.read(); writer.appendNode(lxTerminal, reader.lookup(bm)); } else throw EParseError(bm.column, bm.row); writer.closeNode(); } else throw EParseError(bm.column, bm.row); }
void VMTapeParser:: parseStatement(_ScriptReader& reader, ScriptBookmark& bm, TapeWriter& writer) { if (reader.compare("*")) { bm = reader.read(); writeArgument(writer, bm.state, reader.lookup(bm)); } else if (reader.compare("=")) { bm = reader.read(); writeArray(writer, bm.state, reader.lookup(bm)); } else if (reader.compare("^")) { bm = reader.read(); writeMessage(writer, reader.lookup(bm), SEND_TAPE_MESSAGE_ID); } else if (reader.compare("%")) { bm = reader.read(); ident_t message = reader.lookup(bm); if (message.find('.') != NOTFOUND_POS) { writeExtension(writer, reader.lookup(bm), PUSHE_TAPE_MESSAGE_ID); } else writeMessage(writer, reader.lookup(bm), PUSHM_TAPE_MESSAGE_ID); } else writeObject(writer, bm.state, reader.lookup(bm)); }
bool normalLetterApplyRule(CFParser::Rule& rule, ScriptBookmark& bm, _ScriptReader& reader, CFParser*) { ident_t value = reader.lookup(bm); return (value[0] >= 'A' && value[0] <= 'Z') || (value[0] >= 'a' && value[0] <= 'z'); }
bool normalEOLApplyRule(CFParser::Rule& rule, ScriptBookmark& bm, _ScriptReader& reader, CFParser*) { ident_t value = reader.lookup(bm); return (value[0] == '\n'); }
void CFParser :: defineGrammarRule(_ScriptReader& reader, ScriptBookmark& bm, Rule& rule, ref_t ruleId) { // read: terminal [nonterminal] ; // read: nonterminal [nonterminal2] ; rule.type = rtNormal; int applyMode = 0; while (!reader.compare(";") || bm.state == dfaQuote) { if (bm.state == dfaQuote) { if (rule.terminal) { rule.nonterminal = defineGrammarRule(reader, bm, ruleId, rule.nonterminal); break; } else if (rule.nonterminal) { rule.type = rtChomski; rule.terminal = defineGrammarRule(reader, bm, ruleId); break; } else rule.terminal = writeBodyText(reader.lookup(bm)); } else if (reader.compare("<=")) { saveScript(reader, rule, applyMode); } else if (bm.state == dfaPrivate) { if (rule.terminal) { rule.nonterminal = defineGrammarRule(reader, bm, ruleId, rule.nonterminal); break; } else if (rule.nonterminal) { rule.type = rtChomski; rule.terminal = defineGrammarRule(reader, bm, ruleId); break; } else { rule.terminal = -1; // rule.prefixPtr = defineDSARule(token, reader); if (reader.compare(LITERAL_KEYWORD)) { applyMode = LITERAL_MODE; } else if (reader.compare(NUMERIC_KEYWORD)) { applyMode = NUMERIC_MODE; } else if (reader.compare(EPS_KEYWORD)) { if (rule.nonterminal) throw EParseError(bm.column, bm.row); rule.nonterminal = -1; rule.terminal = -1; applyMode = IDLE_MODE; } else if (reader.compare(EOF_KEYWORD)) { applyMode = EOF_MODE; } else if (reader.compare(REFERENCE_KEYWORD)) { applyMode = REFERENCE_MODE; } else if (reader.compare(IDENTIFIER_KEYWORD)) { applyMode = IDENTIFIER_MODE; } else if (reader.compare(EOL_KEYWORD)) { applyMode = EOL_MODE; } else if (reader.compare(ANYCHR_KEYWORD)) { applyMode = LETTER_MODE; } } } else if (bm.state == dfaIdentifier) { if (rule.nonterminal == 0) { //rule.prefixPtr = defineDSARule(token, reader); rule.nonterminal = mapRuleId(reader.lookup(bm)); } else if (rule.terminal == 0) { rule.type = rtChomski; rule.terminal = mapRuleId(reader.lookup(bm)); } else { if (rule.type == rtChomski) { rule.terminal = defineGrammarRule(reader, bm, ruleId, rule.terminal); } else rule.nonterminal = defineGrammarRule(reader, bm, ruleId, rule.nonterminal); break; } } bm = reader.read(); } // rule.postfixPtr = defineDSARule(token, reader); defineApplyRule(rule, applyMode); }
void CFParser :: saveScript(_ScriptReader& reader, Rule& rule, int& mode) { bool prefixMode = false; if (rule.terminal == 0 && rule.nonterminal == 0) { prefixMode = true; rule.prefixPtr = _body.Length(); } else rule.postfixPtr = _body.Length(); MemoryWriter writer(&_body); ScriptBookmark bm = reader.read(); while (!reader.compare("=>") || bm.state == dfaQuote) { if (bm.state == dfaPrivate) { if (rule.saveTo != NULL) throw EParseError(bm.column, bm.row); if (reader.compare(REFERENCE_KEYWORD)) { rule.terminal = -1; rule.saveTo = saveReference; mode = REFERENCE_MODE; } else if (reader.compare(IDENTIFIER_KEYWORD)) { rule.terminal = -1; rule.saveTo = saveReference; mode = IDENTIFIER_MODE; } else if (reader.compare(LITERAL_KEYWORD)) { rule.terminal = -1; rule.saveTo = saveLiteral; mode = LITERAL_MODE; } else if (reader.compare(NUMERIC_KEYWORD)) { rule.terminal = -1; rule.saveTo = saveReference; mode = NUMERIC_MODE; } writer.writeChar((char)0); rule.postfixPtr = _body.Length(); } else if (bm.state == dfaQuote && reader.compare(REFERENCE_KEYWORD)) { rule.terminal = -1; rule.saveTo = saveLiteral; mode = REFERENCE_MODE; } else if (bm.state == dfaQuote && reader.compare(IDENTIFIER_KEYWORD)) { rule.terminal = -1; rule.saveTo = saveLiteral; mode = IDENTIFIER_MODE; } else { ident_t token = reader.lookup(bm); if (bm.state == dfaQuote) { writer.writeChar('\"'); writer.writeLiteral(token, getlength(token)); writer.writeChar('\"'); } else writer.writeLiteral(token, getlength(token)); writer.writeChar(' '); } bm = reader.read(); } writer.writeChar((char)0); }