예제 #1
0
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;
}
예제 #2
0
void TreeScriptParser :: parseScope(_ScriptReader& reader, ScriptBookmark& bm, SyntaxWriter& writer)
{
   bm = reader.read();
   while (!reader.Eof() && !reader.compare(")")) {
      parseStatement(reader, bm, writer);

      bm = reader.read();
   }

}
예제 #3
0
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;
}
예제 #4
0
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;
}
예제 #5
0
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);
}
예제 #6
0
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);
}
예제 #7
0
void saveReference(_ScriptReader& scriptReader, CFParser* parser, ref_t ptr, ScriptLog& log)
{
   ScriptBookmark bm;
   parser->readScriptBookmark(ptr, bm);

   log.write(scriptReader.lookup(bm));
}
예제 #8
0
bool CFParser :: compareToken(_ScriptReader& reader, ScriptBookmark& bm, int rule)
{
   ident_t terminal = reader.lookup(bm);
   ident_t ruleTerminal = getBodyText(rule);

   return terminal.compare(ruleTerminal);
}
예제 #9
0
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;
}
예제 #10
0
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();
}
예제 #11
0
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();
   }
}
예제 #12
0
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);
}
예제 #13
0
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));
}
예제 #14
0
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');
}
예제 #15
0
bool normalEOLApplyRule(CFParser::Rule& rule, ScriptBookmark& bm, _ScriptReader& reader, CFParser*)
{
   ident_t value = reader.lookup(bm);
   return (value[0] == '\n');
}
예제 #16
0
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);
}
예제 #17
0
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);
}