Preprocessor,
}
- public class ParsingException : Exception
- {
- public ParsingException(Parser parser, string txt)
- : base(string.Format("Parser exception ({0},{1}): {2}", parser.currentLine, parser.currentColumn, txt))
- {
- }
- public ParsingException(Parser parser, string txt, Exception innerException)
- : base(txt, innerException)
- {
- txt = string.Format("Parser exception ({0},{1}): {2}", parser.currentLine, parser.currentColumn, txt);
- }
- }
-
#region CTOR
public Parser (CodeBuffer _buffer)
{
#endregion
- protected int currentLine = 0;
- protected int currentColumn = 0;
+ CodeBuffer buffer;
+
+ internal int currentLine = 0;
+ internal int currentColumn = 0;
protected Token currentTok;
protected bool eof = true;
- CodeBuffer buffer;
-
public List<TokenList> Tokens;
- protected TokenList TokensLine;
public Point CurrentPosition { get { return new Point (currentLine, currentColumn); } }
- public virtual void SetLineInError(int lineNumber) {
+ public abstract void Parse(int line);
+ public virtual void SetLineInError(ParsingException ex) {
currentTok = default(Token);
- Tokens [lineNumber] = new TokenList () {new Token () { Content = buffer [lineNumber] }};
+ Tokens [ex.Line] = new TokenList () {new Token () { Content = buffer [ex.Line] }};
}
- public abstract void Parse(int line);
+ #region low level parsing
protected void readToCurrTok(bool startOfTok = false){
if (startOfTok)
currentTok.Start = CurrentPosition;
currentTok += Read();
}
-
protected void readAndResetCurrentTok(System.Enum type, bool startToc = false) {
readToCurrTok ();
saveAndResetCurrentTok (type);
protected void saveAndResetCurrentTok(System.Enum type) {
currentTok.Type = (TokenType)type;
currentTok.End = CurrentPosition;
- TokensLine.Add (currentTok);
-
+ Tokens[currentLine].Add (currentTok);
currentTok = default(Token);
}
-
protected virtual char Peek() {
if (eof)
throw new ParsingException (this, "Unexpected End of File");
currentColumn++;
return c;
}
-
protected virtual string ReadUntil (string endExp){
string tmp = "";
}
throw new ParsingException (this, string.Format("Expectign '{0}'", endExp));
}
-
protected void SkipWhiteSpaces () {
if (currentTok.Type != TokenType.Unknown)
throw new ParsingException (this, "current token should be reset to unknown (0) before skiping white spaces");
if (currentTok.Type != TokenType.Unknown)
saveAndResetCurrentTok ();
}
+ #endregion
}
}
\ No newline at end of file
namespace Crow.Coding
{
- public struct TextFormating {
- public Color Foreground;
- public Color Background;
-
- public TextFormating(Color fg, Color bg){
- Foreground = fg;
- Background = bg;
- }
- }
/// <summary>
/// Scrolling text box optimized for monospace fonts, for coding
/// </summary>
#region CTOR
public SourceEditor ():base()
{
- formating.Add ((int)XMLParser.TokenType.AttributeName, new TextFormating (Color.DarkBlue, Color.Transparent));
- formating.Add ((int)XMLParser.TokenType.ElementName, new TextFormating (Color.DarkRed, Color.Transparent));
- formating.Add ((int)XMLParser.TokenType.ElementStart, new TextFormating (Color.Red, Color.Transparent));
- formating.Add ((int)XMLParser.TokenType.ElementEnd, new TextFormating (Color.Red, Color.Transparent));
- formating.Add ((int)XMLParser.TokenType.ElementClosing, new TextFormating (Color.Red, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.AttributeName, new TextFormatting (Color.DarkBlue, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.ElementName, new TextFormatting (Color.DarkRed, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.ElementStart, new TextFormatting (Color.Red, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.ElementEnd, new TextFormatting (Color.Red, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.ElementClosing, new TextFormatting (Color.Red, Color.Transparent));
- formating.Add ((int)XMLParser.TokenType.AttributeValueOpening, new TextFormating (Color.DarkPink, Color.Transparent));
- formating.Add ((int)XMLParser.TokenType.AttributeValueClosing, new TextFormating (Color.DarkPink, Color.Transparent));
- formating.Add ((int)XMLParser.TokenType.AttributeValue, new TextFormating (Color.DarkPink, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.AttributeValueOpening, new TextFormatting (Color.DarkPink, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.AttributeValueClosing, new TextFormatting (Color.DarkPink, Color.Transparent));
+ formatting.Add ((int)XMLParser.TokenType.AttributeValue, new TextFormatting (Color.DarkPink, Color.Transparent));
- buffer = new CodeTextBuffer ();
+ buffer = new CodeBuffer ();
buffer.LineUpadateEvent += Buffer_LineUpadateEvent;
buffer.LineAdditionEvent += Buffer_LineAdditionEvent;;
buffer.LineRemoveEvent += Buffer_LineRemoveEvent;
}
#endregion
- Dictionary<int,TextFormating> formating = new Dictionary<int, TextFormating>();
public event EventHandler TextChanged;
#region private and protected fields
int visibleLines = 1;
int visibleColumns = 1;
- CodeTextBuffer buffer;
+ CodeBuffer buffer;
Parser parser;
Color selBackground;
Color selForeground;
Point _selBegin = -1; //selection start (row,column)
Point _selRelease = -1; //selection end (row,column)
+ Dictionary<int,TextFormatting> formatting = new Dictionary<int, TextFormatting>();
+
protected Rectangle rText;
protected FontExtents fe;
protected TextExtents te;
#endregion
- [XmlAttributeAttribute][DefaultValue("label")]
- public string Text
- {
- get {
- return buffer == null ? "" : buffer.FullText;
- }
- set
- {
- if (string.Equals (value, buffer?.FullText, StringComparison.Ordinal))
- return;
-
- buffer.Load (value);
-
- MaxScrollY = Math.Max (0, buffer.Length - visibleLines);
- MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns);
-
- OnTextChanged (this, null);
- RegisterForGraphicUpdate ();
- }
- }
-
- void Buffer_BufferCleared (object sender, EventArgs e)
- {
- parser = new XMLParser (buffer);
- RegisterForGraphicUpdate ();
- }
void reparseSource () {
for (int i = 0; i < parser.Tokens.Count; i++) {
if (parser.Tokens[i].Dirty)
void tryParseBufferLine(int lPtr) {
try {
parser.Parse (lPtr);
- } catch (Exception ex) {
+ } catch (ParsingException ex) {
Debug.WriteLine (ex.ToString ());
- parser.SetLineInError (lPtr);
+ parser.SetLineInError (ex);
}
RegisterForGraphicUpdate ();
}
+ #region Buffer events handlers
+ void Buffer_BufferCleared (object sender, EventArgs e)
+ {
+ parser = new XMLParser (buffer);
+ RegisterForGraphicUpdate ();
+ }
void Buffer_LineAdditionEvent (object sender, CodeBufferEventArgs e)
{
for (int i = 0; i < e.LineCount; i++) {
reparseSource ();
RegisterForGraphicUpdate ();
}
+ #endregion
+
+ #region Public Crow Properties
+ [XmlAttributeAttribute][DefaultValue("label")]
+ public string Text
+ {
+ get {
+ return buffer == null ? "" : buffer.FullText;
+ }
+ set
+ {
+ if (string.Equals (value, buffer?.FullText, StringComparison.Ordinal))
+ return;
+ buffer.Load (value);
+
+ MaxScrollY = Math.Max (0, buffer.Length - visibleLines);
+ MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns);
+
+ OnTextChanged (this, null);
+ RegisterForGraphicUpdate ();
+ }
+ }
[XmlAttributeAttribute][DefaultValue("BlueGray")]
public virtual Color SelectionBackground {
get { return selBackground; }
}
[XmlIgnore]public bool selectionIsEmpty
{ get { return SelRelease == SelBegin; } }
+ #endregion
+ #region Editing and moving cursor
/// <summary>
/// Moves cursor one char to the left.
/// </summary>
}
OnTextChanged (this, null);
}
-
- #region GraphicObject overrides
- public override Font Font {
- get { return base.Font; }
- set {
- base.Font = value;
-
- using (ImageSurface img = new ImageSurface (Format.Argb32, 1, 1)) {
- using (Context gr = new Context (img)) {
- gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
- gr.SetFontSize (Font.Size);
-
- fe = gr.FontExtents;
- }
- }
- MaxScrollY = 0;
- RegisterForGraphicUpdate ();
- }
- }
- protected override int measureRawSize(LayoutingType lt)
+ /// <summary>
+ /// Insert new string at caret position, should be sure no line break is inside.
+ /// </summary>
+ /// <param name="str">String.</param>
+ protected void Insert(string str)
{
- if (lt == LayoutingType.Height)
- return (int)Math.Ceiling(fe.Height * buffer.Length) + Margin * 2;
-
- return (int)(fe.MaxXAdvance * buffer.longestLineCharCount) + Margin * 2;
+ if (!selectionIsEmpty)
+ this.DeleteChar ();
+ string[] strLines = Regex.Split (str, "\r\n|\r|\n|" + @"\\n").ToArray();
+ buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[0]);
+ CurrentColumn += strLines[0].Length;
+ for (int i = 1; i < strLines.Length; i++) {
+ InsertLineBreak ();
+ buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[i]);
+ CurrentColumn += strLines[i].Length;
+ }
+ OnTextChanged (this, null);
+ RegisterForGraphicUpdate();
}
- public override void OnLayoutChanges (LayoutingType layoutType)
+ /// <summary>
+ /// Insert a line break.
+ /// </summary>
+ protected void InsertLineBreak()
{
- base.OnLayoutChanges (layoutType);
-
- if (layoutType == LayoutingType.Height)
- updateVisibleLines ();
- else if (layoutType == LayoutingType.Width)
- updateVisibleColumns ();
+ buffer.Insert(CurrentLine + 1, buffer[CurrentLine].Substring(CurrentColumn));
+ buffer [CurrentLine] = buffer [CurrentLine].Substring (0, CurrentColumn);
+ CurrentLine++;
+ CurrentColumn = 0;
+ OnTextChanged (this, null);
}
+ #endregion
+ #region Drawing
void draw(Context gr){
gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
gr.SetFontSize (Font.Size);
Color selbg = this.SelectionBackground;
Color selfg = this.SelectionForeground;
- if (formating.ContainsKey ((int)tokens [t].Type)) {
- TextFormating tf = formating [(int)tokens [t].Type];
+ if (formatting.ContainsKey ((int)tokens [t].Type)) {
+ TextFormatting tf = formatting [(int)tokens [t].Type];
bg = tf.Background;
fg = tf.Foreground;
}
lPtr += lstr.Length;
}
}
+ #endregion
+
+ #region GraphicObject overrides
+ public override Font Font {
+ get { return base.Font; }
+ set {
+ base.Font = value;
+
+ using (ImageSurface img = new ImageSurface (Format.Argb32, 1, 1)) {
+ using (Context gr = new Context (img)) {
+ gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
+ gr.SetFontSize (Font.Size);
+
+ fe = gr.FontExtents;
+ }
+ }
+ MaxScrollY = 0;
+ RegisterForGraphicUpdate ();
+ }
+ }
+ protected override int measureRawSize(LayoutingType lt)
+ {
+ if (lt == LayoutingType.Height)
+ return (int)Math.Ceiling(fe.Height * buffer.Length) + Margin * 2;
+
+ return (int)(fe.MaxXAdvance * buffer.longestLineCharCount) + Margin * 2;
+ }
+ public override void OnLayoutChanges (LayoutingType layoutType)
+ {
+ base.OnLayoutChanges (layoutType);
+
+ if (layoutType == LayoutingType.Height)
+ updateVisibleLines ();
+ else if (layoutType == LayoutingType.Width)
+ updateVisibleColumns ();
+ }
+
protected override void onDraw (Context gr)
{
base.onDraw (gr);
System.Diagnostics.Debug.WriteLine ("update visible columns: " + visibleColumns);
System.Diagnostics.Debug.WriteLine ("update MaxScrollX: " + MaxScrollX);
}
-
-
-
- /// <summary>
- /// Insert new string at caret position, should be sure no line break is inside.
- /// </summary>
- /// <param name="str">String.</param>
- protected void Insert(string str)
- {
- if (!selectionIsEmpty)
- this.DeleteChar ();
- string[] strLines = Regex.Split (str, "\r\n|\r|\n|" + @"\\n").ToArray();
- buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[0]);
- CurrentColumn += strLines[0].Length;
- for (int i = 1; i < strLines.Length; i++) {
- InsertLineBreak ();
- buffer [CurrentLine] = buffer [CurrentLine].Insert (CurrentColumn, strLines[i]);
- CurrentColumn += strLines[i].Length;
- }
- OnTextChanged (this, null);
- RegisterForGraphicUpdate();
- }
-
- /// <summary>
- /// Insert a line break.
- /// </summary>
- protected void InsertLineBreak()
- {
- buffer.Insert(CurrentLine + 1, buffer[CurrentLine].Substring(CurrentColumn));
- buffer [CurrentLine] = buffer [CurrentLine].Substring (0, CurrentColumn);
- CurrentLine++;
- CurrentColumn = 0;
- OnTextChanged (this, null);
- }
}
}
\ No newline at end of file