From cd2a66a6440eeddf8eaad5209dcdb100eae49d11 Mon Sep 17 00:00:00 2001 From: Tom Robinson Date: Sat, 18 Oct 2008 22:42:16 -0700 Subject: [PATCH] objjc now checks syntax of preprocessed code. --- Objective-J/static.js | 16 +++++++++++++++- Tools/objjc/objjc.js | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Objective-J/static.js b/Objective-J/static.js index 215b10fc4..7fe5ab2aa 100644 --- a/Objective-J/static.js +++ b/Objective-J/static.js @@ -9,7 +9,7 @@ var STATIC_MAGIC_NUMBER = "@STATIC", var STATIC_EXTENSION = "sj"; -function objj_preprocess_file(aFilePath, fileContents) +function objj_preprocess_file(aFilePath, fileContents, checkSyntax) { // Preprocess contents into fragments. var fragments = objj_preprocess(fileContents, { path:"/x" }, { path:aFilePath}), @@ -25,7 +25,21 @@ function objj_preprocess_file(aFilePath, fileContents) if (IS_FILE(fragment)) preprocessed += (IS_LOCAL(fragment) ? MARKER_IMPORT_LOCAL : MARKER_IMPORT_STD) + ';' + GET_PATH(fragment).length + ';' + GET_PATH(fragment); else + { + if (checkSyntax) + { + try + { + new Function(GET_CODE(fragment)); + } + catch (e) + { + e.fragment = fragment; + throw e; + } + } preprocessed += MARKER_CODE + ';' + GET_CODE(fragment).length + ';' + GET_CODE(fragment); + } } return preprocessed; diff --git a/Tools/objjc/objjc.js b/Tools/objjc/objjc.js index 8e62eb75a..442e0ea54 100644 --- a/Tools/objjc/objjc.js +++ b/Tools/objjc/objjc.js @@ -25,7 +25,7 @@ function exec(command) return result; } -function preprocess(aFilePath, outFilePath, gccArgs, shouldObjjPreprocess) +function preprocess(aFilePath, outFilePath, gccArgs, shouldObjjPreprocess, shouldCheckSyntax) { print("Statically Preprocessing " + aFilePath); @@ -58,12 +58,33 @@ function preprocess(aFilePath, outFilePath, gccArgs, shouldObjjPreprocess) fileContents += reader.readLine() + '\n'; reader.close(); + + var results; + + try + { + results = objj_preprocess_file(new File(aFilePath).getName(), fileContents, shouldCheckSyntax); + } + catch (e) + { + if (e.fragment) + { + var lines = e.fragment.info.split("\n"), + PAD = 3; + System.out.println( + "Syntax error in "+e.fragment.file.path+ + " on preprocessed line number "+e.lineNumber+"\n"+ + "\t"+lines.slice(e.lineNumber-1-PAD<0 ? 0 : e.lineNumber-1-PAD, e.lineNumber+PAD).join("\n\t")); + } + else + System.out.println("Unknown error: " + e); + + System.exit(1); + } // Write file. var writer = new BufferedWriter(new FileWriter(outFilePath)); - - writer.write(objj_preprocess_file(new File(aFilePath).getName(), fileContents)); - + writer.write(results); writer.close(); } @@ -77,7 +98,8 @@ function main() gccArgs = [], - shouldObjjPreprocess = true; + shouldObjjPreprocess = true, + shouldCheckSyntax = true; for (; index < count; ++index) { @@ -95,13 +117,16 @@ function main() else if (args[index].indexOf("-E") == 0) shouldObjjPreprocess = false; + + else if (args[index].indexOf("-S") == 0) + shouldCheckSyntax = false; else filePaths.push(args[index]); } for (index = 0, count = filePaths.length; index < count; ++index) - preprocess(filePaths[index], outFilePaths[index], gccArgs, shouldObjjPreprocess); + preprocess(filePaths[index], outFilePaths[index], gccArgs, shouldObjjPreprocess, shouldCheckSyntax); } args = arguments;