Пример #1
0
void LanguagePack::ParseLine(IStringReader *reader)
{
    SkipWhitespace(reader);

    codepoint_t codepoint;
    if (reader->TryPeek(&codepoint))
    {
        switch (codepoint) {
        case '#':
            SkipToEndOfLine(reader);
            break;
        case '[':
            ParseGroupObject(reader);
            break;
        case '<':
            ParseGroupScenario(reader);
            break;
        case '\r':
        case '\n':
            break;
        default:
            ParseString(reader);
            break;
        }
        SkipToEndOfLine(reader);
        SkipNewLine(reader);
    }
}
Пример #2
0
static void ReadMatData(
    Shape&      mat,    // out
    int         nrows,   // in
    int         ncols,   // in
    FILE*       file,    // in
    const char* path)    // in: for error reporting
{
    double* data = Buf(mat);
    for (int i = 0; i < ncols * nrows; i++)
    {
        // skip comments and white space
        int c = ' ';
        while (c == ' ' || c == '\t' || c == '\n' || c == '\r')
        {
            c = fgetc(file);
            if (c == '#') // comment
            {
                SkipToEndOfLine(file, path);
                c = fgetc(file);
            }
        }
        if (c == EOF)
            PrematureEndOfFile(file, path);
        else
        {
            ungetc(c, file);
            float temp; // microsoft compiler can't sscanf doubles so use float
            if (!fscanf(file, "%g", &temp))
                Err("%s(%d): Cannot read %dx%d matrix",
                    path, LineNbr(file), nrows, ncols);
            data[i] = temp;
        }
    }
}
Пример #3
0
void ReadInputNetwork()
{ int i, j, node;
  FILE *inp;
  if (NULL == (inp = fopen(INPUT_FILE_NAME, "r"))) { printf("error in opening %s\n", INPUT_FILE_NAME); exit(1); }
  fscanf(inp, "%s", networkName, inp); SkipToEndOfLine(inp);
  fscanf(inp, "%d", &numNodes); SkipToEndOfLine(inp);
  degrees = (int *)malloc(numNodes * sizeof(int));
  adjNodes = (int **)calloc(numNodes, sizeof(int *));  
  adjNodePositions = (int **)malloc(numNodes * sizeof(int *));
  for (i=0; i<numNodes; i++) {
      fscanf(inp, "%d ", &node); fscanf(inp, "(%d):", &degrees[node]);
      adjNodes[node] = (int *)malloc(degrees[node] * sizeof(int));    // variably sized array of int arrays
      adjNodePositions[node] = (int *)malloc(numNodes * sizeof(int)); // numNodes x numNodes
      for (j=0; j<degrees[node]; j++) 
      { fscanf(inp, "%d", &adjNodes[node][j]);           // tracks jth neighbor of node (variably sized)
        adjNodePositions[node][adjNodes[node][j]] = j;   // given node and neighbor j, returns j's index in *adjNodes[node]
      }
      SkipToEndOfLine(inp);
  }
  fscanf(inp, "%d %d %d %d %lf %lf", &source, &sourceTTL, &maxDestNodeTTL, &numPackets, &probSuccTransm, &RRTdelayFactor); 
SkipToEndOfLine(inp);
  fscanf(inp, "%d", &numPSFQruns); SkipToEndOfLine(inp);
  fscanf(inp, "%d %d %d %d", &RRT_YES_NO, &NEED_NODETTL_POSITIVE_FOR_T_AND_RT, &PRINT_DETAILS, &STEP_EACH_TIME_POINT); SkipToEndOfLine(inp); 
  SkipToEndOfLine(inp);  //keep this; datafile has an extra line before next input PRINT_JUST_FINAL_TOTALS
  if (1 == STEP_EACH_TIME_POINT) PRINT_DETAILS = 1;
  fscanf(inp, "%d", &PRINT_JUST_FINAL_TOTALS); SkipToEndOfLine(inp); 
  if (1 == PRINT_JUST_FINAL_TOTALS) PRINT_DETAILS = STEP_EACH_TIME_POINT = 0;
  fscanf(inp, "%d", &PRINT_TIME_PROFILE_ONE_ITEM_PER_LINE); SkipToEndOfLine(inp); 
  fclose(inp);
  //print input data
  printf("networkName: %s, numNodes=%d, source=%d, sourceTTL=%d, maxDestNodeTTL=%d, numPackets=%d, probSuccTransm=%0.2f, numPSFQruns=%d,\n",
         networkName, numNodes, source, sourceTTL, maxDestNodeTTL, numPackets, probSuccTransm, numPSFQruns);
  printf("           : RRTdelayFactor=%0.2f\n", RRTdelayFactor);
  for (i=0; i<numNodes; i++) {
      printf("%2d(%2d):", i, degrees[i]);
      for (j=0; j<degrees[i]; j++)
          printf(" %d", adjNodes[i][j]);
      printf("\n");
  }
  printf("numPSFQruns(includes unsuccessful runs, if any)=%d\n", numPSFQruns); printf("-----------------------------------\n");
}
Пример #4
0
static bool ReadMat(  // true if read the mat, false if no (more) mats in file
    char*       base, // out: basename in tag
    unsigned&   bits, // out: hex bits in tag
    Shape&      mat,  // out: the matrix
    FILE*       file, // in:  pointer to the shape file
    const char* path) // in:  for error messages
{
    char s[SLEN];     // the string tag before the matrix
    while (1)
    {
        int c = fgetc(file);
        if (c == EOF)
            return false;   // note return
        if (c == '{')
            break;          // note break
        else if (c == '#')
            SkipToEndOfLine(file, path);
        else if (c == '\n' || c == '\r' || c == '\t' || c == ' ') // white space
            ;
        else if (c == '"') // old format tag (enclosed in quotes)
            ;
        else    // any other char, assume it is the start of the tag
        {
            s[0] = char(c);
            if (!Fgets(s+1, SLEN-1, file))
                Err("%s(%d): Read failed (premature EOF)",
                    path, LineNbr(file));
            // remove trailing white space and final quote if any
            int i = STRNLEN(s, SLEN) - 1;
            CV_Assert(i >= 4);
            while (s[i] == ' ' || s[i] == '\t' || s[i] == '"')
                i--;
            s[i+1] = 0;
        }
    }
    if (!s[0])
        Err("%s(%d): Empty tag", path, LineNbr(file));
    if (s[4] != ' ' && s[8] != ' ') // hex string must be 4 or 8 chars
        Err("%s(%d): Malformed tag", path, LineNbr(file));
    if (2 != sscanf(s, "%x %s", &bits, base))
        Err("%s(%d): Malformed tag", path, LineNbr(file));

    int nrows, ncols; int c;
    if (2 != fscanf(file, "%d %d", &nrows, &ncols))
        Err("%s(%d): Cannot read matrix size", path, LineNbr(file));
    if (ncols < 1 || nrows > MAX_MAT_DIM)
        Err("%s(%d): Invalid number of rows %d", path, LineNbr(file), nrows);
    if (ncols < 1 || ncols > MAX_MAT_DIM)
        Err("%s(%d): Invalid number of columns %d", path, LineNbr(file), ncols);

    mat.create(nrows, ncols);
    ReadMatData(mat, nrows, ncols, file, path);

    // make sure that next non-white char is matrix terminator '}'

    c = ' ';
    while (c == ' ' || c == '\t' || c == '\n' || c == '\r') // skip white space
        if (EOF == (c = fgetc(file))) // assignment is intentional
            Err("%s(%d): Cannot read matrix\n"
                "       Reached EOF before finding \"}\"",
                path, LineNbr(file));
    if (c == '#')
        Err("%s(%d): Comment not allowed here", path, LineNbr(file));
    if (c != '}')
        Err("%s(%d): Footer is not \"}\" "
            "(too many or two few entries in matrix?)", path, LineNbr(file));

    return true; // success
}
Пример #5
0
NNT Lexer::NextOperator() {
  switch (*cursor) {
  case '`': IncrementCursor(); RETURN_NNT("`", op_bl, 1);
  case '@': IncrementCursor(); RETURN_NNT("@", op_l, 1);
  case ',': IncrementCursor(); RETURN_NNT(",", comma, 1);
  case ';': IncrementCursor(); RETURN_NNT(";", semicolon, 1);
  case '(': IncrementCursor(); RETURN_NNT("(", l_paren, 1);
  case ')': IncrementCursor(); RETURN_NNT(")", r_paren, 1);
  case '[': IncrementCursor(); RETURN_NNT("[", l_bracket, 1);
  case ']': IncrementCursor(); RETURN_NNT("]", r_bracket, 1);
  case '{': IncrementCursor(); RETURN_NNT("{", l_brace, 1);
  case '}': IncrementCursor(); RETURN_NNT("}", r_brace, 1);
  case '$': IncrementCursor(); RETURN_NNT("$", op_l, 1);

  case '.': {
    Cursor cursor_copy  = cursor;

    // Note: safe because we know we have a null-terminator
    while (*cursor == '.') { IncrementCursor(); }
    size_t num_dots = cursor.offset - cursor_copy.offset;

    if (num_dots == 1) {
      RETURN_NNT(".", op_b, 1);
    } else {
      if (num_dots > 2) { ErrorLog::TooManyDots(cursor_copy, num_dots); }
      RETURN_NNT("..", dots, 2);
    }
  } break;

  case '\\': {
    Cursor cursor_copy = cursor;
    size_t dist = 1;

    IncrementCursor();
    ++dist;
    switch(*cursor) {
    case '\\':
      IncrementCursor();
      RETURN_NNT("", newline, 0);
      break;
    case '\0':
      // Ignore the following newline
      IncrementCursor();
      return Next();
    case ' ':
    case '\t':
      while (IsWhitespace(*cursor)) {
        IncrementCursor();
        ++dist;
      }
      if (*cursor == '\0') {
        IncrementCursor();
        return Next();
      }

    // Intentionally falling through. Looking at a non-whitespace after a '\'
    default:
      ErrorLog::NonWhitespaceAfterNewlineEscape(cursor_copy, dist);
      return Next();
    }
  } break;

  case '#': {
    IncrementCursor();
    Cursor cursor_copy = cursor;

    if (!IsAlpha(*cursor)) {
      ErrorLog::InvalidHashtag(cursor_copy);
      return Next();
    }

    do { IncrementCursor(); } while (IsAlphaNumericOrUnderscore(*cursor));

    if (cursor.offset - cursor_copy.offset == 0) {
      ErrorLog::InvalidHashtag(cursor_copy);
    }

    char old_char       = *cursor;
    *cursor             = '\0';
    const char *tag_ref = cursor.line.ptr + cursor_copy.offset;
    size_t tag_len      = strlen(tag_ref);
    char *tag           = new char[tag_len + 1];
    strcpy(tag, tag_ref);
    *cursor             = old_char;

    RETURN_NNT(tag, hashtag, tag_len + 1);
  } break;

  case '+':
  case '%':
  case '<': 
  case '>': 
  case '|':
  case '^': {
    char first_char = *cursor;
    IncrementCursor();

    char *token = new char[3];
    token[0] = first_char;
    if (*cursor == '=') {
      IncrementCursor();
      token[1] = '=';
    } else {
      token[1] = '\0';
    }
    token[2] = '\0';

    RETURN_NNT(token, op_b, strlen(token));
  } break;

  case '*':
    IncrementCursor();
    if (*cursor == '/') {
      IncrementCursor();
      if (*cursor == '/') {
        // Looking at "*//" which should be parsed as an asterisk followed by a
        // one-line comment.
        BackUpCursor();
        RETURN_NNT("*", op_b, 1);
      } else {
        Cursor cursor_copy = cursor;
        cursor_copy.offset -= 2;
        ErrorLog::NotInMultilineComment(cursor_copy);
        return Next();
      }
    } else if (*cursor == '=') {
      IncrementCursor();
      RETURN_NNT("*=", op_b, 2);
    } else {
      RETURN_NNT("*", op_b, 1);
    }

  case '&': {
    IncrementCursor();
    if (*cursor == '=') {
      IncrementCursor();
      RETURN_NNT("&=", op_b, 2);
    } else {
      RETURN_NNT("&", op_bl, 1);
    }
  } break;

  case ':':  {
    IncrementCursor();

    if (*cursor == '=') {
      IncrementCursor();
      RETURN_NNT(":=", op_b, 2);

    } else if (*cursor == '>') {
      IncrementCursor();
      RETURN_NNT(":>", op_b, 2);

    } else {
      RETURN_NNT(":", colon, 1);
    }
  } break;

  case '!': {
    IncrementCursor();
    if (*cursor == '=') {
      IncrementCursor();
      RETURN_NNT("!=", op_b, 2);
    } else {
      RETURN_NNT("!", op_l, 1);
    }
  } break;

  case '-': {
    IncrementCursor();
    if (*cursor == '=') {
      IncrementCursor();
      RETURN_NNT("-=", op_b, 2);

    } else if (*cursor == '>') {
      IncrementCursor();
      auto nptr = new AST::TokenNode(cursor, "->");
      nptr->op = Language::Operator::Arrow;
      return NNT(nptr, Language::fn_arrow);

    } else if (*cursor == '-') {
      IncrementCursor();
      RETURN_TERMINAL(Hole, Unknown, IR::Value::None());

    } else {
      RETURN_NNT("-", op_bl, 1);
    }
  } break;

  case '=': {
    IncrementCursor();
    if (*cursor == '=') {
      IncrementCursor();
      RETURN_NNT("==", op_b, 2);

    } else if (*cursor == '>') {
      IncrementCursor();
      RETURN_NNT("=>", op_b, 2);

    } else {
      RETURN_NNT("=", eq, 1);
    }
  } break;

  case '/': {
    IncrementCursor();
    if (*cursor == '/') {
      // Ignore comments altogether
      SkipToEndOfLine();
      return Next();

    } else if (*cursor == '=') {
      IncrementCursor();
      RETURN_NNT("/=", op_b, 2);

    } else if (*cursor == '*') {
      IncrementCursor();
      char back_one = *cursor;
      IncrementCursor();

      size_t comment_layer = 1;

      while (comment_layer != 0) {
        if (ifs.eof()) {
          ErrorLog::RunawayMultilineComment();
          RETURN_NNT("", eof, 0);

        } else if (back_one == '/' && *cursor == '*') {
          ++comment_layer;

        } else if (back_one == '*' && *cursor == '/') {
          --comment_layer;
        }

        back_one = *cursor;
        IncrementCursor();
      }

      // Ignore comments altogether
      return Next();

    } else {
      RETURN_NNT("/", op_b, 1);
    }

  } break;

  case '"': {
    IncrementCursor();

    std::string str_lit = "";

    while (*cursor != '"' && *cursor != '\0') {
      if (*cursor == '\\') {
        IncrementCursor();
        switch (*cursor) {
        case '\'': {
          str_lit += '\'';
          Cursor cursor_copy = cursor;
          --cursor_copy.offset;
          ErrorLog::EscapedSingleQuoteInStringLit(cursor_copy);
        } break;

        case '\\': str_lit += '\\'; break;
        case '"':  str_lit += '"';  break;
        case 'a':  str_lit += '\a'; break;
        case 'b':  str_lit += '\b'; break;
        case 'f':  str_lit += '\f'; break;
        case 'n':  str_lit += '\n'; break;
        case 'r':  str_lit += '\r'; break;
        case 't':  str_lit += '\t'; break;
        case 'v':  str_lit += '\v'; break;

        default: {
          Cursor cursor_copy = cursor;
          --cursor_copy.offset;
          ErrorLog::InvalidEscapeCharInStringLit(cursor_copy);

          str_lit += *cursor;
        } break;
        }
      } else {
        str_lit += *cursor;
      }

      IncrementCursor();
    }

    if (*cursor == '\0') {
      ErrorLog::RunawayStringLit(cursor);
    } else {
      IncrementCursor();
    }

    // Not leaked. It's owned by a terminal which is persistent.
    char *cstr = new char[str_lit.size() + 2];
    strcpy(cstr + 1, str_lit.c_str());
    cstr[0] = '\1';
    RETURN_TERMINAL(StringLiteral, String, IR::Value(cstr));
  } break;

  case '\'': {
    IncrementCursor();
    char result;

    switch (*cursor) {
    case '\t':
      ErrorLog::TabInCharLit(cursor);
      result = '\t';
      break;

    case '\0': {
      ErrorLog::RunawayCharLit(cursor);

      RETURN_TERMINAL(Char, Char, IR::Value::Char('\0'));
    }
    case '\\': {
      IncrementCursor();
      switch (*cursor) {
      case '\"': {
        result = '"';
        Cursor cursor_copy = cursor;
        --cursor_copy.offset;
        ErrorLog::EscapedDoubleQuoteInCharLit(cursor_copy);
      } break;
      case '\\': result = '\\'; break;
      case '\'': result = '\''; break;
      case 'a': result  = '\a'; break;
      case 'b': result  = '\b'; break;
      case 'f': result  = '\f'; break;
      case 'n': result  = '\n'; break;
      case 'r': result  = '\r'; break;
      case 't': result  = '\t'; break;
      case 'v': result  = '\v'; break;
      default:
        Cursor cursor_copy = cursor;
        --cursor_copy.offset;
        ErrorLog::InvalidEscapeCharInCharLit(cursor_copy);
        result = *cursor;
      }
      break;
    }
    default: { result = *cursor; } break;
    }

    IncrementCursor();

    if (*cursor == '\'') {
      IncrementCursor();
    } else {
      ErrorLog::RunawayCharLit(cursor);
    }

    RETURN_TERMINAL(Char, Char, IR::Value::Char(result));
  } break;

  case '?':
    ErrorLog::InvalidCharQuestionMark(cursor);
    IncrementCursor();
    return Next();

  case '~':
    ErrorLog::InvalidCharTilde(cursor);
    IncrementCursor();
    return Next();

  case '_': UNREACHABLE;
  default: UNREACHABLE;
  }
}