private string ConvertScriptFile(BuildType type, string inputPath) { string result = null; string outputPath = null; try { outputPath = Path.Combine(OutputDirectory, Path.ChangeExtension(Path.GetFileName(inputPath), ".js")); PreprocessorOptions options = new PreprocessorOptions(); options.SourceFile = new FileInputStreamSource(inputPath); options.TargetFile = new FileOutputStreamSource(outputPath); options.DebugFlavor = Debug; options.Minimize = Minimize; options.StripCommentsOnly = StripCommentsOnly; options.UseWindowsLineBreaks = WindowsLineBreaks; options.PreprocessorVariables = _preprocessorVariables; ScriptPreprocessor preprocessor; if (type == BuildType.ScriptAssembly) { preprocessor = new ScriptPreprocessor(this, this); } else { preprocessor = new ScriptPreprocessor(); } preprocessor.Preprocess(options); result = outputPath; } catch (Exception e) { #if DEBUG Log.LogErrorFromException(e, /* showStackTrace */ true); #else Log.LogErrorFromException(e, /* showStackTrace */ false); #endif // DEBUG } if ((result == null) && File.Exists(outputPath)) { try { File.Delete(outputPath); } catch { } } return(result); }
public static List <TokenDefinition> Tokenize(string input, ICollection <TokenDefinition> definition, char[] quote_types, bool include_whitespace, bool include_newlines, PreprocessorOptions preopts) { // First run preprocessor passes // Do trigraph replacement if required if (preopts.StandardCTrigraphs) { input = Replace(input, new string[][] { new string[] { "??=", "#" }, new string[] { "??/", "\\" }, new string[] { "??'", "^" }, new string[] { "??(", "[" }, new string[] { "??)", "]" }, new string[] { "??!", "|" }, new string[] { "??<", "{" }, new string[] { "??>", "}" }, new string[] { "??-", "~" }, }, false); } List <TokenDefinition> ret = new List <TokenDefinition>(); if (quote_types == null) { quote_types = new char[] { } } ; char current_quote_type = '\0'; StringBuilder cur_str = new StringBuilder(); int i = 0; // Terminate input with a newline input = input + "\n"; while (i < input.Length) { char c = input[i]; // If we're within a quote, do interpret it if (current_quote_type != '\0') { // Is it the end of the quote? if (c == current_quote_type) { ret.Add(new TokenDefinition(cur_str.ToString())); ret.Add(new TokenDefinition(current_quote_type)); i++; cur_str = new StringBuilder(); current_quote_type = '\0'; continue; } // Is it an escaped character? if (c == '\\') { i++; c = input[i]; cur_str.Append(c); i++; continue; } // Else, add it to the current string cur_str.Append(c); i++; continue; } // Not within a quote // Is it the start of a quote? foreach (char quote_start in quote_types) { if (c == quote_start) { // We're at the start of a quote. AddToken(ret, cur_str.ToString(), include_whitespace, include_newlines); current_quote_type = c; cur_str = new StringBuilder(); i++; continue; } } // Not the start of a quote, is it whitespace? if (IsWhiteSpace(c)) { // Is the current token only whitespace? If so add it if (IsWhiteSpace(cur_str.ToString())) { cur_str.Append(c); i++; continue; } // If not, it marks the end of a string if (cur_str.Length > 0) { // Now try and match the specific strings against this string s = cur_str.ToString(); int j = 0; int str_start = 0; while (j < s.Length) { int chars_left = s.Length - j; bool match = false; foreach (TokenDefinition spec_str in definition) { if ((spec_str.Type == TokenDefinition.def_type.String) && (chars_left >= spec_str.String.Length)) { if (s.Substring(j, spec_str.String.Length) == spec_str.String) { // We have a match // Add the previous string if there was one if (str_start != j) { AddToken(ret, s.Substring(str_start, j - str_start), include_whitespace, include_newlines); } // Add this match AddToken(ret, spec_str.String, include_whitespace, include_newlines); // Advance on beyond this string j = j + spec_str.String.Length; str_start = j; match = true; break; } } } if (!match) { j++; } } // Now add whatever's left if (str_start < s.Length) { AddToken(ret, s.Substring(str_start), include_whitespace, include_newlines); } cur_str = new StringBuilder(); cur_str.Append(c); i++; continue; } } // Its just a regular character // Is the current string just whitespace? if (IsWhiteSpace(cur_str.ToString())) { AddToken(ret, cur_str.ToString(), include_whitespace, include_newlines); cur_str = new StringBuilder(); } cur_str.Append(c); i++; continue; } // Now remove comments i = 0; int starting_comment_token = 0; PreprocessorOptions.CommentsType cur_comment = null; while (i < ret.Count) { TokenDefinition tok = ret[i]; // Are we currently in a comment? if (cur_comment != null) { // Do we match the end comment marker? if (((cur_comment.End == null) && (tok.Type == TokenDefinition.def_type.Newline)) || ((cur_comment.End == tok.String) && (tok.Type == TokenDefinition.def_type.String))) { // Remove tokens from starting_comment_token up to and including i ret.RemoveRange(starting_comment_token, i - starting_comment_token + 1); i = starting_comment_token; // Reinsert the newline if we removed one if (cur_comment.End == null) { ret.Insert(i, new TokenDefinition(TokenDefinition.def_type.Newline)); i++; } cur_comment = null; continue; } else { i++; continue; } } else { // Do we match a new comment start? if ((tok.Type == TokenDefinition.def_type.String) && (preopts.Comments != null)) { foreach (PreprocessorOptions.CommentsType ct in preopts.Comments) { if (ct.Start == tok.String) { cur_comment = ct; starting_comment_token = i; } } } } // Continue i++; } // If still in a comment, remove the last tokens if (cur_comment != null) { ret.RemoveRange(starting_comment_token, i - starting_comment_token); } // Now parse looking for #define, #include etc // First add a newline at the start if not already there, to assist searching on 'Newline' '#' 'include' etc if (!((ret.Count > 0) && (ret[0].Type == TokenDefinition.def_type.Newline))) { ret.Insert(0, new TokenDefinition(TokenDefinition.def_type.Newline)); } i = 0; while (i < ret.Count) { // TODO if (preopts.Includes && Match(ret, PreprocessorOptions.IncludeDefinition, i)) { } i++; } // Now remove the newline again ret.RemoveAt(0); // Terminate with a newline ret.Add(new TokenDefinition(TokenDefinition.def_type.Newline)); return(ret); }
private string ConvertScriptFile(BuildType type, string inputPath) { string result = null; string outputPath = null; try { outputPath = Path.Combine(OutputDirectory, Path.ChangeExtension(Path.GetFileName(inputPath), ".js")); PreprocessorOptions options = new PreprocessorOptions(); options.SourceFile = new FileInputStreamSource(inputPath); options.TargetFile = new FileOutputStreamSource(outputPath); options.DebugFlavor = Debug; options.Minimize = Minimize; options.StripCommentsOnly = StripCommentsOnly; options.UseWindowsLineBreaks = WindowsLineBreaks; options.PreprocessorVariables = _preprocessorVariables; ScriptPreprocessor preprocessor; if (type == BuildType.ScriptAssembly) { preprocessor = new ScriptPreprocessor(this, this); } else { preprocessor = new ScriptPreprocessor(); } preprocessor.Preprocess(options); result = outputPath; } catch (Exception e) { #if DEBUG Log.LogErrorFromException(e, /* showStackTrace */ true); #else Log.LogErrorFromException(e, /* showStackTrace */ false); #endif // DEBUG } if ((result == null) && File.Exists(outputPath)) { try { File.Delete(outputPath); } catch { } } return result; }
public static List <TokenDefinition> Tokenize(string input, TokenGrammar grammar, PreprocessorOptions preopts) { return(Tokenize(input, grammar.Definition, grammar.Quotes, grammar.IncludeWhitespace, grammar.IncludeNewlines, preopts)); }