void OnValidate()
        {
            Tokenizer tok = new Tokenizer();

            if (Application.isPlaying)
            {
                CodeConvert.TryFill(values, ref dict, null, tok);
            }
            else
            {
                CodeConvert.TryParse(values, out dict, null, tok);
            }
            if (tok.errors.Count > 0)
            {
                parseResults = tok.errors.JoinToString("\n");
            }
            else
            {
                //parseResults = dict.Show(true);
                string newResults = dict.Stringify(pretty: true, showType: true, showNulls: true);
                if (parseResults != newResults)
                {
                    parseResults = newResults;
                }
            }
        }
        public string Format(string text)
        {
            Tokenizer tok          = new Tokenizer();
            string    resolvedText = CodeConvert.Format(text, dict, tok);

            if (tok.HasError())
            {
                Show.Error(tok.errors.JoinToString(", "));
            }
            return(resolvedText);
        }
        public float NumValue(string fieldName)
        {
            object val;

            if (!dict.TryGetValue(fieldName, out val))
            {
                return(0);
            }
            CodeConvert.TryConvert(ref val, typeof(float));
            return((float)val);
        }
 void Awake()
 {
     Global.GetComponent <ScriptedDictionaryManager>().Register(this);
     if (!string.IsNullOrEmpty(values))
     {
         Tokenizer tok = new Tokenizer();
         CodeConvert.TryParse(values, out dict, null, tok);
         if (tok.HasError())
         {
             Show.Warning(tok.GetErrorString());
         }
     }
 }
Пример #5
0
        void OnValidate()
        {
            List <ParseError> errors = new List <ParseError>();

            CodeConvert.TryFill(values, ref dict, errors);
            if (errors.Count > 0)
            {
                parseResults = string.Join("\n", errors.ConvertAll(e => e.ToString()).ToArray());
            }
            else
            {
                //parseResults = dict.Show(true);
                parseResults = Show.Stringify(dict, true);
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            string filepath =
                //"../../../Program.cs";
                //"../../../testdata.txt";
                "../../../dialogs.txt";
            string text = System.IO.File.ReadAllText(filepath);
            //IList<string> tokens = StringParse.Tokenize(text);
            //List<Token> tokens = new List<Token>();
            //List<int> rows = new List<int>();
            //List<ParseError> errors = new List<ParseError>();
            //Dictionary<string, float> dict;
            //CodeConvert.TryParse(text, out dict, errors);
            //Show.Log(Show.Stringify(dict, true));
            //errors.ForEach(e => Show.Log(e.ToString()));
            //Tokenizer.Tokenize(text, tokens, rows, errors);
            //bool parsed = CodeConvert.TryParse(text, out TestData testData, errors);
            Dictionary <string, float> dict = new Dictionary <string, float>()
            {
                ["number"] = 10.0f,
                ["xp"]     = 1000
            };
            Tokenizer tokenizer = new Tokenizer();
            bool      parsed    = CodeConvert.TryParse(text, out Dialog[] testData, dict, tokenizer);

            Console.WriteLine(Show.Stringify(testData, true));
            if (tokenizer.errors.Count > 0)
            {
                Show.Error(tokenizer.errors.Join("\n"));
                tokenizer.errors.Clear();
                Console.ReadKey();
            }
            TestData td = new TestData();

            td.number = 3;
            Dialog lastDialog = testData[testData.Length - 1];

            Dialog.DialogOption[] opt = lastDialog.options;
            Expression            ex  = opt[opt.Length - 1].requires;

//			Console.WriteLine(ex.DebugPrint());
            Console.WriteLine(ex.Resolve(tokenizer, dict).Join(", "));
            if (tokenizer.errors.Count > 0)
            {
                Show.Error(tokenizer.errors.Join("\n"));
                Console.ReadKey();
            }

            Show.Log(CodeConvert.Format("hello world {xp} more text {number > 2*8} wat {{hey}} wat", dict));

            return;

            List <Token> tokens = tokenizer.tokens;
            List <int>   rows   = tokenizer.rows;

            for (int i = 0; i < tokens.Count; ++i)
            {
                Console.ForegroundColor = ((i % 2) == 0) ? ConsoleColor.White : ConsoleColor.Green;
                Console.Write(i + "~ " + tokens[i].index + "@" + ParseError.FilePositionOf(tokens[i], rows) + ": ");
                if (tokens[i].meta is Context.Entry e)
                {
                    if (e.IsText() || e.IsComment())
                    {
                        Console.Write(e.TextRaw);
                        i += e.tokenCount - 1;                      //e.IndexAfter(tokens, i);
                    }
                    else
                    {
                        Console.Write(tokens[i]);
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.Write(" " + ParseError.FilePositionOf(e.GetBeginToken(), rows) + " -> " + ParseError.FilePositionOf(e.GetEndToken(), rows) +
                                      " " + e.context.name + " depth:" + e.depth);
                    }
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.Write(" " + e.tokenCount + "tokens ");
                    //for (int t = 0; t < e.tokenCount; ++t) { Console.Write("'" + tokens[t + e.tokenStart] + "' "); }
                }
                else
                {
                    Console.Write(tokens[i]);
                }
                Console.WriteLine();
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }