123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- /**
- *
- * @author seagle
- * @date 2024-7-10
- * @Description 从字符串中读取关键字
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MECF.Framework.UI.Core.DxfScript
- {
- public class StringReader
- {
- private static readonly char CH_ROUND_LEFT = '(';
- private static readonly char CH_ROUND_RIGHT = ')';
- private static readonly char CH_BRACE_LEFT = '{';
- private static readonly char CH_BRACE_RIGHT = '}';
- private static readonly char CH_PLUS = '+';
- private static readonly char CH_MINUS = '-';
- private static readonly char CH_MULTIPLE = '*';
- private static readonly char CH_DEVIDE = '/';
- private static readonly char CH_CHANGE = '\\';
- private static readonly char CH_EQUAL = '=';
- private static readonly char CH_EXCLAM = '!';
- private static readonly char CH_WAVE = '~';
- private static readonly char CH_GT = '>';
- private static readonly char CH_LT = '<';
- private static readonly char CH_DOT = '.';
- private static readonly char CH_AND = '&';
- private static readonly char CH_OR = '|';
- private static readonly char CH_COMMA = ',';
- private static readonly char CH_SEP = ';';
- private static readonly char CH_QUOT = '"';
- private string scriptData;
- private KeywordType lastKwType;
- public int __line__;
- private int scriptLength = 0;
- private int scriptOffset = 0;
- private string buffer;
- public StringReader(string scriptData)
- {
- this.scriptData = scriptData;
- if( scriptData == null)
- {
- this.scriptData = "";
- }
- __line__ = 1;
- scriptLength = scriptData.Length;
- scriptOffset = 0;
- buffer = "";
- lastKwType = KeywordType.KW_INVALID;
- }
- private static char EOF = (char)0xffff;
- private char Getc()
- {
- if (scriptData == null)
- {
- return EOF;
- }
- if(scriptOffset >= scriptLength)
- {
- return EOF;
- }
- char ch = scriptData[scriptOffset++];
- return ch;
- }
- private void UnGetc()
- {
- if (scriptData == null)
- {
- return ;
- }
- if (scriptOffset > scriptLength)
- {
- return ;
- }
- if (scriptOffset <=0)
- {
- return;
- }
- scriptOffset--;
-
- }
- private bool Eof()
- {
- if (scriptData == null) return true;
- return scriptOffset >= scriptLength;
- }
- private bool isAlpha(char ch)
- {
- if (ch >= 'a' && ch <= 'z')
- {
- return true;
- }
- else if (ch >= 'A' && ch <= 'Z')
- {
- return true;
- }
- else if (ch >= '0' && ch <= '9')
- {
- return true;
- }
- else if (ch == '_')
- {
- return true;
- }
- return false;
- }
- public void rollback(KeywordType kwType)
- {
- lastKwType = kwType;
- }
- public KeywordType Next()
- {
- if (lastKwType != KeywordType.KW_INVALID)
- {
- KeywordType retKwType = lastKwType;
- lastKwType = KeywordType.KW_INVALID;
- return retKwType;
- }
- if (Eof())
- {
- return KeywordType.KW_EOF;
- }
-
- char ch;
- while ((ch = Getc()) != EOF)
- {
- if (ch == ' ' || ch == '\t' || ch == 0x0d)
- {
- continue;
- }
- if (ch == 0x0a)
- {
- __line__++;
- continue;
- }
- if (ch == EOF)
- {
- return KeywordType.KW_EOF;
- }
- else if (ch == CH_DEVIDE)
- {
- char ch2 = Getc();
- if (ch2 == CH_DEVIDE)
- {
- char ch3;
- while ((ch3 = Getc()) != EOF)
- {
- if (ch3 == 0x0a)
- {
- __line__++;
- break;
- }
- }
- if (ch3 == EOF)
- {
- return KeywordType.KW_EOF;
- }
- else
- {
- continue;
- }
- }
- else if (ch2 == CH_MULTIPLE)
- {
- char ch3;
- while ((ch3 = Getc()) != EOF)
- {
- if (ch3 == 0x0a)
- {
- __line__++;
- }
- else if (ch3 == CH_MULTIPLE)
- {
- char ch4 = Getc();
- if (ch4 == CH_DEVIDE)
- {
- break;
- }
- else if (ch4 == 0x0a)
- {
- __line__++;
- }
- }
- }
- if (ch3 == EOF)
- {
- return KeywordType.KW_EOF;
- }
- else
- {
- continue;
- }
- }
- else
- {
- //real devide
- UnGetc();
- buffer = CH_DEVIDE.ToString();
- return KeywordType.KW_OPERATOR_DEVIDE;
- }
- }
- else if (ch == CH_SEP)
- {
- buffer = CH_SEP.ToString();
- return KeywordType.KW_SEP;
- }
- else if (ch == CH_QUOT)
- {
- buffer = "";
- char ch2;
- while ((ch2 = Getc())!= EOF)
- {
- if (ch2 == CH_QUOT)
- {
- return KeywordType.KW_CONST_STRING;
- }
- else if (ch2 == CH_CHANGE)
- {
- char ch3 = Getc();
- if (ch3 == EOF)
- {
- return KeywordType.KW_INVALID;
- }
- else if (ch3 == CH_QUOT)
- {
- buffer+= ch3.ToString();
- }
- else if (ch3 == 'n')
- {
- buffer += "\n";
- }
- else if (ch3 == 't')
- {
-
- buffer+= '\t';
- }
- else if (ch3 == '\\')
- {
-
- buffer+= ch3.ToString();
- }
- else
- {
- return KeywordType.KW_INVALID;
- }
- }
- else if (ch2 == 0x0a)
- {
- return KeywordType.KW_INVALID;
- }
- else
- {
-
- buffer+= ch2.ToString();
- }
- }
- return KeywordType.KW_INVALID;
- }
- else if (ch == CH_PLUS)
- {
- buffer = ch.ToString();
- return KeywordType.KW_OPERATOR_PLUS;
- }
- else if (ch == CH_MULTIPLE)
- {
- buffer = ch.ToString();
- return KeywordType.KW_OPERATOR_MULTIPLE;
- }
- else if (ch == CH_MINUS)
- {
- char ch_last = buffer[0];
- if (ch_last == CH_ROUND_RIGHT || isAlpha(ch_last))
- {
- buffer= ch.ToString();
- return KeywordType.KW_OPERATOR_MINUS;
- }
- else
- {
- buffer = ch.ToString();
- return KeywordType.KW_OPERATOR_NEGATIVE;
- }
- }
- else if (ch == CH_COMMA)
- {
- buffer = ch.ToString();
- return KeywordType.KW_OPERATOR_COMMA;
- }
- else if (ch >= '0' && ch <= '9')
- {
- char ch2;
- bool hexFlag = false;
- bool dotFlag = false;
- buffer = ch.ToString();
-
- while ((ch2 = Getc()) != EOF)
- {
- if (ch2 == 'x' || ch2 == 'X')
- {
- if (buffer.Equals("0") && !hexFlag)
- {
- hexFlag = true;
-
- buffer="";
- }
- else
- {
- return KeywordType.KW_INVALID;
- }
- }
- else if (ch2 == CH_DOT)
- {
- if (!dotFlag)
- {
- dotFlag = true;
- buffer += ch2.ToString();
-
- }
- else
- {
- return KeywordType.KW_INVALID;
- }
- }
- else if (ch2 >= '0' && ch2 <= '9')
- {
- buffer += ch2.ToString();
- }
- else if (ch2 >= 'a' && ch2 <= 'f')
- {
-
-
- if (hexFlag)
- {
- buffer+= ch2.ToString();
- }
- else
- {
- return KeywordType.KW_INVALID;
- }
-
- }
- else if (ch2 >= 'A' && ch2 <= 'F')
- {
- if (hexFlag)
- {
- buffer += ch2.ToString();
- }
- else
- {
- return KeywordType.KW_INVALID;
- }
- }
- else
- {
- UnGetc();
- if (hexFlag && dotFlag)
- {
- return KeywordType.KW_INVALID;
- }
- else if (dotFlag)
- {
- return KeywordType.KW_CONST_DECIMAL;
- }
- else
- {
- if (hexFlag && buffer.Length<3)
- {
- return KeywordType.KW_INVALID;
- }
- if (hexFlag)
- {
- buffer = ""+int.Parse(buffer, System.Globalization.NumberStyles.HexNumber);
- }
- return KeywordType.KW_CONST_INTEGER;
- }
- }
- }
- return KeywordType.KW_INVALID;
- }
- else if (isAlpha(ch))
- {
- char ch2;
- buffer = ch.ToString();
- bool dotFlag = false;
- while ((ch2 = Getc()) != EOF)
- {
- if (isAlpha(ch2))
- {
- buffer+= ch2.ToString();
- }
- else if (ch2 == CH_DOT)
- {
- buffer += ch2.ToString();
- dotFlag = true;
- }
- else
- {
- UnGetc();
- if (buffer.ToLower().Equals("on") ||
- buffer.ToLower().Equals("true"))
- {
- return KeywordType.KW_CONST_TRUE;
- }
- else if (buffer.ToLower().Equals("off") ||
- buffer.ToLower().Equals("false"))
- {
- return KeywordType.KW_CONST_FALSE;
- }
- else if (buffer.ToLower().Equals("if"))
- {
- return KeywordType.KW_IF;
- }
- else if (buffer.ToLower().Equals("else"))
- {
- return KeywordType.KW_ELSE;
- }else if(buffer.ToLower().Equals("return"))
- {
- return KeywordType.KW_RETURN;
- }
- else if (buffer.ToLower().Equals( "when"))
- {
- return KeywordType.KW_WHEN;
- }
- else if (buffer.ToLower().Equals( "for"))
- {
- return KeywordType.KW_FOR;
- }
- else if (ch2 == CH_ROUND_LEFT)
- {
- return KeywordType.KW_FUNCTION;
- }
- else
- {
- return KeywordType.KW_VARIABLE;
- }
- }
- }
- return KeywordType.KW_INVALID;
- }
- else if (ch == CH_GT)
- {
- char ch2 = Getc();
- if (ch2 == EOF)
- {
- return KeywordType.KW_INVALID;
- }
- else if (ch2 == CH_GT)
- {
- buffer = ">>";
- return KeywordType.KW_OPERATOR_MOVE_RIGHT;
- }
- else if (ch2 == CH_EQUAL)
- {
- buffer = ">=";
- return KeywordType.KW_OPERATOR_GE;
- }
- else
- {
- buffer = ">";
- UnGetc();
- return KeywordType.KW_OPERATOR_GT;
- }
- }
- else if (ch == CH_LT)
- {
- char ch2 = Getc();
- if (ch2 == EOF)
- {
- return KeywordType.KW_INVALID;
- }
- else if (ch2 == CH_LT)
- {
- buffer = "<<";
- return KeywordType.KW_OPERATOR_MOVE_LEFT;
- }
- else if (ch2 == CH_EQUAL)
- {
- buffer = "<=";
- return KeywordType.KW_OPERATOR_LE;
- }
- else
- {
- buffer = "<";
- UnGetc();
- return KeywordType.KW_OPERATOR_LT;
- }
- }
- else if (ch == CH_EXCLAM)
- {
- char ch2 = Getc();
- if (ch2 == EOF)
- {
- return KeywordType.KW_INVALID;
- }
- else if (ch2 == CH_EQUAL)
- {
- buffer = "!=";
- return KeywordType.KW_OPERATOR_NOT_EQUAL;
- }
- else
- {
- buffer = "!";
- return KeywordType.KW_OPERATOR_NOT;
- }
- }
- else if (ch == CH_EQUAL)
- {
- char ch2 = Getc();
- if (ch2 == EOF)
- {
- return KeywordType.KW_INVALID;
- }
- else if (ch2 == CH_EQUAL)
- {
- buffer = "==";
- return KeywordType.KW_OPERATOR_EQUAL;
- }
- else
- {
- buffer = "=";
- UnGetc();
- return KeywordType.KW_SET;
- }
- }
- else if (ch == CH_AND)
- {
- char ch2 = Getc();
- if (ch2 == EOF)
- {
- return KeywordType.KW_INVALID;
- }
- else if (ch2 == CH_AND)
- {
- buffer = "&&";
- return KeywordType.KW_OPERATOR_AND;
- }
- else
- {
- buffer = "&";
- return KeywordType.KW_OPERATOR_BIT_AND;
- }
- }
- else if (ch == CH_OR)
- {
- char ch2 = Getc();
- if (ch2 == EOF)
- {
- return KeywordType.KW_INVALID;
- }
- else if (ch2 == CH_OR)
- {
- buffer = "||";
- return KeywordType.KW_OPERATOR_OR;
- }
- else
- {
- buffer = "|";
- UnGetc();
- return KeywordType.KW_OPERATOR_BIT_OR;
- }
- }
- else if (ch == CH_WAVE)
- {
- buffer = CH_WAVE.ToString();
- return KeywordType.KW_OPERATOR_BIT_NOT;
- }
- else if (ch == CH_ROUND_LEFT)
- {
- buffer = CH_ROUND_LEFT.ToString ();
-
- return KeywordType.KW_ROUND_LEFT;
- }
- else if (ch == CH_ROUND_RIGHT)
- {
- buffer= CH_ROUND_RIGHT.ToString();
-
- return KeywordType.KW_ROUND_RIGHT;
- }
- else if (ch == CH_BRACE_LEFT)
- {
- buffer= CH_BRACE_LEFT.ToString();
-
- return KeywordType.KW_BRACE_LEFT;
- }
- else if (ch == CH_BRACE_RIGHT)
- {
- buffer= CH_BRACE_RIGHT.ToString();
-
- return KeywordType.KW_BRACE_RIGHT;
- }
- else
- {
- return KeywordType.KW_INVALID;
- }
- break;
- }
- return KeywordType.KW_EOF;
- }
- public string Data()
- {
- return buffer;
- }
- }
- }
|