forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseTreeTools.cs
94 lines (83 loc) · 2.94 KB
/
ParseTreeTools.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Xna.Framework;
namespace TwoMGFX
{
public static class ParseTreeTools
{
public static float ParseFloat(string value)
{
// Remove all whitespace and trailing F or f.
value = value.Replace(" ", "");
value = value.TrimEnd('f', 'F');
return float.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture);
}
public static int ParseInt(string value)
{
// We read it as a float and cast it down to
// an integer to match Microsoft FX behavior.
return (int)Math.Floor(ParseFloat(value));
}
public static bool ParseBool(string value)
{
if (value.ToLowerInvariant() == "true" || value == "1")
return true;
if (value.ToLowerInvariant() == "false" || value == "0")
return false;
throw new Exception("Invalid boolean value '" + value + "'");
}
public static Color ParseColor(string value)
{
var hexValue = Convert.ToUInt32(value, 16);
byte r, g, b, a;
if (value.Length == 8)
{
r = (byte) ((hexValue >> 16) & 0xFF);
g = (byte) ((hexValue >> 8) & 0xFF);
b = (byte) ((hexValue >> 0) & 0xFF);
a = 255;
}
else if (value.Length == 10)
{
r = (byte) ((hexValue >> 24) & 0xFF);
g = (byte) ((hexValue >> 16) & 0xFF);
b = (byte) ((hexValue >> 8) & 0xFF);
a = (byte) ((hexValue >> 0) & 0xFF);
}
else
{
throw new NotSupportedException();
}
return new Color(r, g, b, a);
}
public static void WhitespaceNodes(TokenType type, List<ParseNode> nodes, ref string sourceFile)
{
for (var i = 0; i < nodes.Count; i++)
{
var n = nodes[i];
if (n.Token.Type != type)
{
WhitespaceNodes(type, n.Nodes, ref sourceFile);
continue;
}
// Get the full content of this node.
var start = n.Token.StartPos;
var end = n.Token.EndPos;
var length = end - n.Token.StartPos;
var content = sourceFile.Substring(start, length);
// Replace the content of this node with whitespace.
for (var c = 0; c < length; c++)
{
if (!char.IsWhiteSpace(content[c]))
content = content.Replace(content[c], ' ');
}
// Add the whitespace back to the source file.
var newfile = sourceFile.Substring(0, start);
newfile += content;
newfile += sourceFile.Substring(end);
sourceFile = newfile;
}
}
}
}