bool GrammarRead(Grammar *grammar, const char *fileName) { // Read the file. grammar->fileName = fileName; FILE *srcFile = fopen(fileName, "r"); if (srcFile == NULL) { AllocSprintf(&grammar->errMsg, "can't open '%s'", fileName); return false; } // Go through it line by line. grammar->lineNo = 1; char line[4096]; while (fgets(line, sizeof(line), srcFile)) { // Remove comments. char *commentPos = strstr(line, "//"); if (commentPos != NULL) { *commentPos = 0; } // Remove trailing whitespace. StringStripEnd(line); // Skip empty lines. if (line[0] != 0) { // Is it a definition line? if (isalpha(line[0])) { // Parse a definition. if (!GrammarParseDefinition(grammar, line)) goto grErrExit; } else if (isspace(line[0])) { // Parse an option. if (!GrammarParseOption(grammar, line)) goto grErrExit; } else { // Unexpected character. AllocSprintf(&grammar->errMsg, "unexpected character in %s line %d: '%s'", fileName, grammar->lineNo, line); goto grErrExit; } } grammar->lineNo++; } return true; grErrExit: fclose(srcFile); return false; }
bool GrammarRead(Grammar *grammar, const char *fileName) { // Read the file. grammar->fileName = fileName; if (!ReadFile(&grammar->src, &grammar->errMsg, fileName)) return false; // Go through it line by line. grammar->lineNo = 1; char *line; char *nextLine = grammar->src; while (ReadLine(&line, &nextLine)) { // Remove comments. char *commentPos = strstr(line, "//"); if (commentPos != NULL) { *commentPos = 0; } // Remove trailing whitespace. StringStripEnd(line); // Skip empty lines. if (line[0] != 0) { // Is it a definition line? if (isalnum(line[0])) { // Parse a definition. if (!GrammarParseDefinition(grammar, line)) return false; } else if (isspace(line[0])) { // Parse an option. if (!GrammarParseOption(grammar, line)) return false; } else { // Unexpected character. AllocSprintf(&grammar->errMsg, "unexpected character in %s line %d: '%s'", fileName, grammar->lineNo, line); return false; } } grammar->lineNo++; } return true; }