public string ReportPosition() { DslScript dslScript; int position; if (PositionInTokenList < _tokenList.Count()) { dslScript = CurrentToken.DslScript; position = CurrentToken.PositionInDslScript; } else if (_tokenList.Count > 0) { dslScript = _tokenList.Last().DslScript; position = dslScript.Script.Length; } else { dslScript = new DslScript { Script = "", Name = "", Path = "" }; position = 0; } return(ScriptPositionReporting.ReportPosition(dslScript.Script, position, dslScript.Path)); }
private static string LoadFile(string basicFilePath, DslScript dslScript, int begin) { var filePaths = new List <string> { basicFilePath }; string basicFileExtension = Path.GetExtension(basicFilePath); if (basicFileExtension.Equals(".sql", StringComparison.OrdinalIgnoreCase)) { var directory = Path.GetDirectoryName(basicFilePath); var fileName = Path.GetFileNameWithoutExtension(basicFilePath); if (string.IsNullOrWhiteSpace(fileName)) { throw new DslSyntaxException("Referenced empty file name (" + basicFilePath + ") in DSL script. " + dslScript.ReportPosition(begin)); } // Look for SQL dialect-specific files before the generic SQL file: filePaths.Insert(0, Path.Combine(directory, fileName + "." + SqlUtility.DatabaseLanguage + basicFileExtension)); filePaths.Insert(1, Path.Combine(directory, fileName + " (" + SqlUtility.DatabaseLanguage + ")" + basicFileExtension)); } foreach (var filePath in filePaths) { if (File.Exists(filePath)) { return(File.ReadAllText(filePath, Encoding.Default)); } } throw new DslSyntaxException("Cannot find the extension file referenced in DSL script. " + dslScript.ReportPosition(begin) + "\r\nLooking for files:\r\n" + string.Join("\r\n", filePaths)); }
private static string ReadQuotedString(DslScript dslScript, ref int end) { string script = dslScript.Script; char quote = script[end]; int begin = end; end++; while (true) { while (end < script.Length && script[end] != quote) { end++; } if (end >= script.Length) { throw new DslSyntaxException("Unexpected end of script within quoted string. Missing closing character: " + quote + ". " + dslScript.ReportPosition(begin)); } if (end + 1 < script.Length && script[end + 1] == quote) { // Two quote characters make escape sequence for a quote within the string: end += 2; continue; } else { // Single quote ends string: end++; break; } } return(script.Substring(begin + 1, end - begin - 2).Replace(new string(quote, 2), new string(quote, 1))); }
private static string ReadExternalText(DslScript dslScript, ref int end) { string script = dslScript.Script; int begin = end; end++; while (end < script.Length && script[end] != '>' && !invalidPathChars.Contains(script[end])) { end++; } if (end >= script.Length) { throw new DslSyntaxException("Unexpected end of script within external text reference. Missing closing character: '>'." + dslScript.ReportPosition(end)); } if (script[end] != '>') { throw new DslSyntaxException("Invalid filename character within external text reference. " + dslScript.ReportPosition(end)); } end++; // Skip closing character. string basicFilePath = script.Substring(begin + 1, end - begin - 2); string dslScriptFolder = Path.GetDirectoryName(dslScript.Path); return(LoadFile(Path.Combine(dslScriptFolder, basicFilePath), dslScript, begin)); }
private static string ReadExternalText(DslScript dslScript, ref int end, Func <string, string> readAllTextfromFile) { string script = dslScript.Script; int begin = end; end++; while (end < script.Length && script[end] != '>' && !invalidPathChars.Contains(script[end])) { end++; } if (end >= script.Length) { var errorMessage = "Unexpected end of script within external text reference. Missing closing character: '>'."; throw new DslSyntaxException(errorMessage, "RH0009", dslScript, begin, 0, null); } if (script[end] != '>') { var errorMessage = "Invalid filename character within external text reference."; throw new DslSyntaxException(errorMessage, "RH0010", dslScript, end, 0, null); } end++; // Skip closing character. string basicFilePath = script.Substring(begin + 1, end - begin - 2); string dslScriptFolder = Path.GetDirectoryName(dslScript.Path); return(LoadFile(Path.Combine(dslScriptFolder, basicFilePath), dslScript, begin, readAllTextfromFile)); }
public static Token GetNextToken_ValueType(DslScript dslScript, ref int position) { string script = dslScript.Script; if (position < script.Length && Whitespaces.Contains(script[position])) { throw new FrameworkException("Unexpected call of GetNextToken_ValueType without skipping whitespaces."); } if (IsSimpleStringElement(script[position])) { return new Token { Value = ReadSimpleStringToken(script, ref position), Type = TokenType.Text } } ; else if (IsQuotedStringStart(script[position])) { return new Token { Value = ReadQuotedString(dslScript, ref position), Type = TokenType.Text } } ; else if (IsExternalTextStart(script[position])) { return new Token { Value = ReadExternalText(dslScript, ref position), Type = TokenType.Text } } ; else if (IsSingleLineCommentStart(script, position)) { return new Token { Value = ReadSingleLineComment(script, ref position), Type = TokenType.Comment } } ; else { return new Token { Value = ReadSpecialCharacter(script, ref position), Type = TokenType.Special } }; }
public DslSyntaxException(string message, string errorCode, DslScript dslScript, int positionBegin = 0, int positionEnd = 0, string additionalDetails = null) : base(message) { ErrorCode = errorCode; if (!string.IsNullOrEmpty(dslScript?.Path)) { FilePosition = new FilePosition(dslScript.Path, dslScript.Script, positionBegin, positionEnd); } var detailsList = new List <string>(); if (!string.IsNullOrEmpty(dslScript?.Script)) { detailsList.Add($"Syntax error at \"{ScriptPositionReporting.ReportPreviousAndFollowingTextInline(dslScript.Script, positionBegin)}\""); } if (!string.IsNullOrEmpty(additionalDetails)) { detailsList.Add(additionalDetails); } Details = string.Join("\r\n", detailsList); }
private string ReadExternalText(DslScript dslScript, ref int end, Func <string, string> readAllTextfromFile) { string script = dslScript.Script; int begin = end; end++; while (end < script.Length && script[end] != '>' && !invalidPathChars.Contains(script[end])) { end++; } if (end >= script.Length) { var errorMessage = "Unexpected end of script within external text reference. Missing closing character: '>'."; throw new DslSyntaxException(errorMessage, "RH0009", dslScript, begin, 0, null); } if (script[end] != '>') { var errorMessage = "Invalid filename character within external text reference."; throw new DslSyntaxException(errorMessage, "RH0010", dslScript, end, 0, null); } end++; // Skip closing character. // User can use either Windows or Linux/MacOs path separator, // so that we must convert to cross-platform path separator. string basicFilePath = script .Substring(begin + 1, end - begin - 2) .Replace('\\', Path.DirectorySeparatorChar) .Replace('/', Path.DirectorySeparatorChar); string dslScriptFolder = Path.GetDirectoryName(dslScript.Path); return(LoadFile(Path.Combine(dslScriptFolder, basicFilePath), dslScript, begin, readAllTextfromFile)); }
private string LoadFile(string basicFilePath, DslScript dslScript, int begin, Func <string, string> readAllTextfromFile) { var filePaths = new List <string> { basicFilePath }; string basicFileExtension = Path.GetExtension(basicFilePath); if (basicFileExtension.Equals(".sql", StringComparison.OrdinalIgnoreCase)) { var directory = Path.GetDirectoryName(basicFilePath); var fileName = Path.GetFileNameWithoutExtension(basicFilePath); if (string.IsNullOrWhiteSpace(fileName)) { var errorMessage = $"Referenced empty file name ({basicFilePath}) in DSL script."; throw new DslSyntaxException(errorMessage, "RH0011", dslScript, begin, 0, null); } // Look for SQL dialect-specific files before the generic SQL file: filePaths.Insert(0, Path.Combine(directory, fileName + "." + _syntax.DatabaseLanguage + basicFileExtension)); filePaths.Insert(1, Path.Combine(directory, fileName + " (" + _syntax.DatabaseLanguage + ")" + basicFileExtension)); } foreach (var filePath in filePaths) { if (File.Exists(filePath)) { return(readAllTextfromFile(filePath)); } } var notFoundMessage = "Cannot find the extension file referenced in DSL script."; var fileListMessage = "Looking for files:\r\n" + string.Join("\r\n", filePaths); throw new DslSyntaxException($"{notFoundMessage} {fileListMessage}", "RH0012", dslScript, begin, 0, null); }
private static string ReadQuotedString(DslScript dslScript, ref int end) { string script = dslScript.Script; char quote = script[end]; int begin = end; end++; while (true) { while (end < script.Length && script[end] != quote) end++; if (end >= script.Length) throw new DslSyntaxException("Unexpected end of script within quoted string. Missing closing character: " + quote + ". " + dslScript.ReportPosition(begin)); if (end + 1 < script.Length && script[end + 1] == quote) { // Two quote characters make escape sequence for a quote within the string: end += 2; continue; } else { // Single quote ends string: end++; break; } } return script.Substring(begin + 1, end - begin - 2).Replace(new string(quote, 2), new string(quote, 1)); }
private static string ReadExternalText(DslScript dslScript, ref int end) { string script = dslScript.Script; int begin = end; end++; while (end < script.Length && script[end] != '>' && !invalidPathChars.Contains(script[end])) end++; if (end >= script.Length) throw new DslSyntaxException("Unexpected end of script within external text reference. Missing closing character: '>'." + dslScript.ReportPosition(end)); if (script[end] != '>') throw new DslSyntaxException("Invalid filename character within external text reference. " + dslScript.ReportPosition(end)); end++; // Skip closing character. string basicFilePath = script.Substring(begin + 1, end - begin - 2); string dslScriptFolder = Path.GetDirectoryName(dslScript.Path); return LoadFile(Path.Combine(dslScriptFolder, basicFilePath), dslScript, begin); }
private static string LoadFile(string basicFilePath, DslScript dslScript, int begin) { var filePaths = new List<string> { basicFilePath }; string basicFileExtension = Path.GetExtension(basicFilePath); if (basicFileExtension.Equals(".sql", StringComparison.OrdinalIgnoreCase)) { var directory = Path.GetDirectoryName(basicFilePath); var fileName = Path.GetFileNameWithoutExtension(basicFilePath); if (string.IsNullOrWhiteSpace(fileName)) throw new DslSyntaxException("Referenced empty file name (" + basicFilePath + ") in DSL script. " + dslScript.ReportPosition(begin)); // Look for SQL dialect-specific files before the generic SQL file: filePaths.Insert(0, Path.Combine(directory, fileName + "." + SqlUtility.DatabaseLanguage + basicFileExtension)); filePaths.Insert(1, Path.Combine(directory, fileName + " (" + SqlUtility.DatabaseLanguage + ")" + basicFileExtension)); } foreach (var filePath in filePaths) if (File.Exists(filePath)) return File.ReadAllText(filePath, Encoding.Default); throw new DslSyntaxException("Cannot find the extension file referenced in DSL script. " + dslScript.ReportPosition(begin) + "\r\nLooking for files:\r\n" + string.Join("\r\n", filePaths)); }
public static Token GetNextToken_ValueType(DslScript dslScript, ref int position) { string script = dslScript.Script; if (position < script.Length && Whitespaces.Contains(script[position])) throw new FrameworkException("Unexpected call of GetNextToken_ValueType without skipping whitespaces."); if (IsSimpleStringElement(script[position])) return new Token { Value = ReadSimpleStringToken(script, ref position), Type = TokenType.Text }; else if (IsQuotedStringStart(script[position])) return new Token { Value = ReadQuotedString(dslScript, ref position), Type = TokenType.Text }; else if (IsExternalTextStart(script[position])) return new Token { Value = ReadExternalText(dslScript, ref position), Type = TokenType.Text }; else if (IsSingleLineCommentStart(script, position)) return new Token { Value = ReadSingleLineComment(script, ref position), Type = TokenType.Comment }; else return new Token { Value = ReadSpecialCharacter(script, ref position), Type = TokenType.Special }; }
public string ReportPosition() { DslScript dslScript; int position; if (PositionInTokenList < _tokenList.Count()) { dslScript = CurrentToken.DslScript; position = CurrentToken.PositionInDslScript; } else if (_tokenList.Count > 0) { dslScript = _tokenList.Last().DslScript; position = dslScript.Script.Length; } else { dslScript = new DslScript { Script = "", Name = "", Path = "" }; position = 0; } return ScriptPositionReporting.ReportPosition(dslScript.Script, position, dslScript.Path); }