public bool Parse(Accessibility accessibility, string str) { m_strSpec = str; m_accessibility = accessibility; // Create a scanner StringScanner s = new StringScanner(str); // Parse target spec if (s.current != '.') { m_specTarget = new IdentifierSpec(); if (!m_specTarget.Parse(s)) return false; } // Parse member spec if (s.current == '.') { s.SkipForward(1); if (!s.eof) { // Parse rhs m_specMember = new IdentifierSpec(); if (!m_specMember.Parse(s)) return false; } } else { if (m_specTarget != null) { m_specNonMember = m_specTarget; m_specTarget = null; } else return false; } if (!s.eof) return false; return true; }
public bool Parse(StringScanner s) { // Can't be empty if (s.eof) return false; // Regex? if (s.current == '/') { s.SkipForward(1); s.Mark(); while (s.current != '/') { if (s.eof) return false; if (s.current == '\\') s.SkipForward(2); else s.SkipForward(1); } try { m_regex = new System.Text.RegularExpressions.Regex(s.Extract()); s.SkipForward(1); return true; } catch (Exception) { return false; } } // Wildcard or explicit bool bWildcard = false; bool bLeading = true; s.Mark(); while (!s.eof && s.current != '.') { // Wildcard? if (s.current == '?' || s.current == '*') { bWildcard = true; s.SkipForward(1); bLeading=false; continue; } // Valid identifier character? if (bLeading) { if (!Tokenizer.IsIdentifierLeadChar(s.current)) return false; } else { if (!Tokenizer.IsIdentifierChar(s.current)) return false; } // Next s.SkipForward(1); bLeading = false; } // Extract it string str = s.Extract(); // If it ends with an asterix, it's a wildcard if (bWildcard) { str = str.Replace("*", "(.*)"); str = str.Replace("?", "(.)"); m_regex = new System.Text.RegularExpressions.Regex("^" + str + "$"); return true; } /// Store it m_identifier = str; return true; }
public void OpenIncludeFile(string text, string fileName) { // Register the included file Compiler.RegisterIncludedFile(fileName); // Save the current string parser m_IncludeStack.Push(p); // Prep the string scanner p = new StringScanner(); p.Reset(text); p.FileName = fileName; // Used to detect line breaks between tokens for automatic // semicolon insertion m_bPreceededByLineBreak = true; m_prevTokenEnd = 0; }
// Get the next token, skipping comments as we go public Token Next() { m_prevToken = m_currentToken; do { m_currentToken = ParseToken(); if (m_currentToken == Token.eof && m_IncludeStack.Count > 0) { // Pop include stack p = m_IncludeStack.Pop(); m_prevTokenEnd = p.position; return Next(); } // Check for directive comments if (m_currentToken == Token.comment) { if (m_strIdentifier.StartsWith("!")) { m_currentToken = Token.directive_comment; break; } string str = m_strIdentifier.Trim(); if (str=="fall through") { m_currentToken=Token.kw_fallthrough; break; } if (str == "warnings:on") { m_bWarnings = true; } if (str == "warnings:off") { m_bWarnings = false; } if (str == "warnings:push:on") { Compiler.WarningsEnabledStack.Push(m_bWarnings); m_bWarnings = true; } if (str == "warnings:push:off") { Compiler.WarningsEnabledStack.Push(m_bWarnings); m_bWarnings = false; } if (str == "warnings:pop") { if (Compiler.WarningsEnabledStack.Count > 0) { m_bWarnings = Compiler.WarningsEnabledStack.Pop(); } else { m_bWarnings = true; Compiler.RecordWarning(GetBookmark(), "can't pop warning state."); } } if (str.StartsWith("private:")) { m_currentToken = Token.directive_private; m_strIdentifier = str.Substring(8); break; } if (str.StartsWith("public:")) { m_currentToken = Token.directive_public; m_strIdentifier = str.Substring(7); break; } if (str.StartsWith("include:")) { // Get file name string strFile = str.Substring(8).Trim(); // Work out fully qualified name, relative to current file being processed string strDir = System.IO.Path.GetDirectoryName(p.FileName); string strOldDir = System.IO.Directory.GetCurrentDirectory(); System.IO.Directory.SetCurrentDirectory(strDir); strFile = System.IO.Path.GetFullPath(strFile); System.IO.Directory.SetCurrentDirectory(strOldDir); // Open the include file OpenIncludeFile(System.IO.File.ReadAllText(strFile), strFile); // Recurse return Next(); } } } while (m_currentToken == Token.comment); m_prevTokenEnd = m_tokenEnd; m_tokenEnd = p.position; return m_currentToken; }
// Constructor public Tokenizer(Compiler Compiler, string str, string strFileName, bool bWarnings) { this.Compiler=Compiler; // Prep the string scanner p = new StringScanner(); p.Reset(str); p.FileName = strFileName; m_bWarnings = bWarnings; // Used to detect line breaks between tokens for automatic // semicolon insertion m_bPreceededByLineBreak = true; // Queue up the first token m_prevToken = Token.eof; Next(); }
public Bookmark(StringScanner file, int position, Token token, bool warnings) { this.file = file; this.position = position; this.token = token; this.warnings = warnings; }
public bool Parse(StringScanner s) { // Can't be empty if (s.eof) { return(false); } // Regex? if (s.current == '/') { s.SkipForward(1); s.Mark(); while (s.current != '/') { if (s.eof) { return(false); } if (s.current == '\\') { s.SkipForward(2); } else { s.SkipForward(1); } } try { m_regex = new System.Text.RegularExpressions.Regex(s.Extract()); s.SkipForward(1); return(true); } catch (Exception) { return(false); } } // Wildcard or explicit bool bWildcard = false; bool bLeading = true; s.Mark(); while (!s.eof && s.current != '.') { // Wildcard? if (s.current == '?' || s.current == '*') { bWildcard = true; s.SkipForward(1); bLeading = false; continue; } // Valid identifier character? if (bLeading) { if (!Tokenizer.IsIdentifierLeadChar(s.current)) { return(false); } } else { if (!Tokenizer.IsIdentifierChar(s.current)) { return(false); } } // Next s.SkipForward(1); bLeading = false; } // Extract it string str = s.Extract(); // If it ends with an asterix, it's a wildcard if (bWildcard) { str = str.Replace("*", "(.*)"); str = str.Replace("?", "(.)"); m_regex = new System.Text.RegularExpressions.Regex("^" + str + "$"); return(true); } /// Store it m_identifier = str; return(true); }