overnord / aml

Simple parser for the AML Markup Language.

Clone this repository (size: 162.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/overnord/aml/
commit 54: 177f222c7e69
parent 52: 6dc110861564
branch: csharp
created csharp branch
bje...@lambda
8 months ago

Changed (Δ10.8 KB):

raw changeset »

aml.sln (29 lines added, 0 lines removed)

aml/aml.csproj (53 lines added, 0 lines removed)

aml/node.cs (245 lines added, 0 lines removed)

aml/parser.cs (70 lines added, 0 lines removed)

aml/scanner.cs (39 lines added, 0 lines removed)

aml/validator.cs (45 lines added, 0 lines removed)

c/include/aml.h

c/include/node.h

c/include/parser.h

c/include/scanner.h

c/include/string.h

c/makefile

c/src/node.c

c/src/parser.c

c/src/scanner.c

c/src/string.c

c/test.c

csharp/aml.sln

csharp/aml/aml.csproj

csharp/aml/node.cs

csharp/aml/parser.cs

csharp/aml/scanner.cs

csharp/aml/validator.cs

examples/build.py

examples/list.aml

examples/list.py

examples/ui.aml

examples/ui.py

python/2.x/aml/__init__.py

python/2.x/aml/node.py

python/2.x/aml/parser.py

python/2.x/aml/path/__init__.py

python/2.x/aml/path/parser.py

python/2.x/aml/path/path.py

python/2.x/aml/path/scanner.py

python/2.x/aml/pretty.py

python/2.x/aml/scanner.py

python/2.x/setup.py

python/3.x/aml/__init__.py

python/3.x/aml/node.py

python/3.x/aml/parser.py

python/3.x/aml/path/__init__.py

python/3.x/aml/path/parser.py

python/3.x/aml/path/path.py

python/3.x/aml/path/scanner.py

python/3.x/aml/pretty.py

python/3.x/aml/scanner.py

python/3.x/setup.py

python/experimental/query.py

python/experimental/test.aml

python/experimental/validate.aml

python/experimental/validator.aml

python/experimental/validator.py

ruby/aml.rb

Up to file-list aml.sln:

1

2
Microsoft Visual Studio Solution File, Format Version 9.00
3
# Visual Studio 2005
4
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aml", "aml\aml.csproj", "{003790E7-06F5-4529-9149-6AB749345E8C}"
5
EndProject
6
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{9191BA44-53E9-4F67-AD60-6ECEA8708A3C}"
7
EndProject
8
Global
9
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
10
		Debug|Any CPU = Debug|Any CPU
11
		Release|Any CPU = Release|Any CPU
12
	EndGlobalSection
13
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
14
		{003790E7-06F5-4529-9149-6AB749345E8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15
		{003790E7-06F5-4529-9149-6AB749345E8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16
		{003790E7-06F5-4529-9149-6AB749345E8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17
		{003790E7-06F5-4529-9149-6AB749345E8C}.Release|Any CPU.Build.0 = Release|Any CPU
18
		{9191BA44-53E9-4F67-AD60-6ECEA8708A3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19
		{9191BA44-53E9-4F67-AD60-6ECEA8708A3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
20
		{9191BA44-53E9-4F67-AD60-6ECEA8708A3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
21
		{9191BA44-53E9-4F67-AD60-6ECEA8708A3C}.Release|Any CPU.Build.0 = Release|Any CPU
22
	EndGlobalSection
23
	GlobalSection(MonoDevelopProperties) = preSolution
24
		version = 0.1
25
		outputpath = build\bin
26
		StartupItem = aml\aml.csproj
27
		name = aml
28
	EndGlobalSection
29
EndGlobal

Up to file-list aml/aml.csproj:

1
<?xml version="1.0" encoding="utf-8"?>
2
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
  <PropertyGroup>
4
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6
    <ProductVersion>8.0.50727</ProductVersion>
7
    <SchemaVersion>2.0</SchemaVersion>
8
    <ProjectGuid>{003790E7-06F5-4529-9149-6AB749345E8C}</ProjectGuid>
9
    <OutputType>Library</OutputType>
10
    <AssemblyName>aml</AssemblyName>
11
  </PropertyGroup>
12
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
13
    <DebugSymbols>true</DebugSymbols>
14
    <DebugType>full</DebugType>
15
    <Optimize>true</Optimize>
16
    <OutputPath>bin\Debug</OutputPath>
17
    <DefineConstants>DEBUG</DefineConstants>
18
    <ErrorReport>prompt</ErrorReport>
19
    <WarningLevel>4</WarningLevel>
20
    <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
21
    <ConsolePause>false</ConsolePause>
22
    <Execution>
23
      <Execution clr-version="Net_2_0" />
24
    </Execution>
25
  </PropertyGroup>
26
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27
    <DebugType>none</DebugType>
28
    <Optimize>true</Optimize>
29
    <OutputPath>bin\Release</OutputPath>
30
    <ErrorReport>prompt</ErrorReport>
31
    <WarningLevel>4</WarningLevel>
32
    <ConsolePause>false</ConsolePause>
33
    <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
34
    <Execution>
35
      <Execution clr-version="Net_2_0" />
36
    </Execution>
37
  </PropertyGroup>
38
  <ItemGroup>
39
    <Compile Include="node.cs" />
40
    <Compile Include="scanner.cs" />
41
    <Compile Include="parser.cs" />
42
    <Compile Include="path\path.cs" />
43
    <Compile Include="path\scanner.cs" />
44
    <Compile Include="path\parser.cs" />
45
  </ItemGroup>
46
  <ItemGroup>
47
    <Reference Include="System" />
48
  </ItemGroup>
49
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
50
  <ItemGroup>
51
    <Folder Include="path\" />
52
  </ItemGroup>
53
</Project>

Up to file-list aml/node.cs:

1
/*
2
 * 
3
 * aml.node
4
 * ~~~~~~~~
5
 * 
6
 * :copyright: 2009 by Bjoern Schulz <bjoern.schulz.92@gmail.com>
7
 * :license: MIT, see LICENSE for more details 
8
 * 
9
 */
10
11
using System;
12
using System.Collections;
13
using System.Text.RegularExpressions;
14
15
namespace aml.node {
16
	
17
	public interface INode: IEnumerable {
18
		
19
		object this[string name] {
20
			get;
21
			set;
22
		}
23
		
24
		void SetAttribute(string name, object value);
25
		
26
		object GetAttribute(string name);
27
		
28
		void DeleteAttribute(string name);
29
		
30
		IList Children {
31
			get;
32
		}
33
		
34
		Hashtable Attributes {
35
			get;
36
		}
37
		
38
		int Length {
39
			get;
40
		}
41
		
42
		INode Parent {
43
			get;
44
		}
45
		
46
		INode FirstChild {
47
			get;
48
		}
49
		
50
		INode LastChild {
51
			get;
52
		}
53
		
54
		string Name {
55
			set;
56
			get;
57
		}
58
		
59
		IEnumerable Each();
60
		
61
		void Append(Node node);
62
		
63
		void Insert(int index, Node node);
64
		
65
		void Remove(Node node);
66
	}
67
	
68
	public class Node: INode {
69
		
70
		private string _name;
71
		private Node _parent = null;
72
		private Node _first_child = null;
73
		private Node _last_child = null;
74
		private Hashtable _attributes = new Hashtable();
75
		private ArrayList _children = new ArrayList();
76
77
		public Node(string name) {
78
			this._name = name;
79
		}
80
		
81
		public Node(string name, Hashtable attributes) {
82
			this._name = name;
83
			foreach (DictionaryEntry entry in attributes) {
84
				this._attributes[entry.Key] = entry.Value;
85
			}
86
		}
87
		
88
		public Node(string name, IList children) {
89
			this._name = name;
90
			foreach (Node node in children) {
91
				this.Append(node);
92
			}
93
		}
94
		
95
		public Node(string name, Hashtable attributes, IList children) {
96
			this._name = name;
97
			foreach (DictionaryEntry entry in attributes) {
98
				this._attributes[entry.Key] = entry.Value;
99
			}
100
			foreach (Node node in children) {
101
				this.Append(node);
102
			}
103
		}
104
		
105
		public override string ToString() {
106
			string src = "(" + this._name + "{atts}{childs})";
107
			string attributes = "";
108
			foreach(DictionaryEntry entry in this._attributes) {
109
				attributes += " (" + entry.Key + " \"" + entry.Value + "\")";
110
			}
111
			src = src.Replace("{atts}", attributes);
112
			string childs = "";
113
			foreach(Node node in this._children) {
114
				childs += " " + node.ToString();
115
			}
116
			src = src.Replace("{childs}", childs);
117
			return src;
118
		}
119
		
120
		public IEnumerable Each() {
121
			foreach (Node node in this) {
122
				yield return node;
123
			}
124
		}
125
		
126
		public void SetAttribute(string name, object value) {
127
			this._attributes[name] = value;
128
		}
129
		
130
		public object GetAttribute(string name) {
131
			return this._attributes[name];
132
		}
133
		
134
		public void DeleteAttribute(string name) {
135
			this._attributes.Remove(name);
136
		}
137
		
138
		public void Append(Node node) {
139
			this.Insert(this._children.Count, node);
140
		}
141
		
142
		public void Insert(int index, Node node) {
143
			node._parent = node;
144
			this._children.Insert(index, node);
145
			this._first_child = (Node) this._children[0];
146
			this._last_child = (Node) this._children[this.Length - 1];
147
		}
148
		
149
		public void Remove(Node node) {
150
			this._children.Remove(node);
151
			if (this._children.Count > 0) {
152
				this._first_child = (Node) this._children[0];
153
				this._last_child = (Node) this._children[this.Length - 1];
154
			}
155
		}
156
		
157
		public int Length {
158
			get {
159
				return this._children.Count;
160
			}
161
		}
162
		
163
		public INode Parent {
164
			get {
165
				return this._parent;
166
			}
167
		}
168
		
169
		public INode FirstChild {
170
			get {
171
				return this._first_child;
172
			}
173
		}
174
		
175
		public INode LastChild {
176
			get {
177
				return this._last_child;
178
			}
179
		}
180
		
181
		public string Name {
182
			get {
183
				return this._name;
184
			} set {
185
				Regex re = new Regex("^\\w+$");
186
				if (re.IsMatch(value) && value.Length > 0) {
187
					this._name = value;
188
				}
189
			}
190
		}
191
		
192
		public object this[string name] {
193
			get {
194
				return this._attributes[name];
195
			} set {
196
				this._attributes[name] = value;
197
			}
198
		}
199
		
200
		public Hashtable Attributes {
201
			get {
202
				return this._attributes;
203
			}
204
		}
205
		
206
		public IList Children {
207
			get {
208
				return this._children;
209
			}
210
		}
211
		
212
		IEnumerator IEnumerable.GetEnumerator() {
213
			return new NodeEnum(this._children);
214
        }
215
	}
216
	
217
	public class NodeEnum: IEnumerator {
218
		
219
		private ArrayList _children;
220
		private int position = -1;
221
		
222
		public NodeEnum(ArrayList children) {
223
			this._children = children;
224
		}
225
		
226
		public bool MoveNext() {
227
			this.position++;
228
			return (this.position < this._children.Count);
229
		}
230
		
231
		public void Reset() {
232
			this.position--;
233
		}
234
		
235
		public object Current {
236
			get {
237
				try {
238
					return this._children[position];
239
				} catch (IndexOutOfRangeException) {
240
					throw new InvalidOperationException();
241
				}
242
			}
243
		}
244
	}	
245
}

Up to file-list aml/parser.cs:

1
using System;
2
using System.Collections;
3
using System.Text.RegularExpressions;
4
using aml.node;
5
using aml.scanner;
6
7
namespace aml.parser {
8
	
9
	public class Parser {
10
		
11
		private IEnumerator scanner;
12
		private string t;
13
		private object v;
14
		
15
		public Parser(IEnumerable scanner) {
16
			this.scanner = scanner.GetEnumerator();
17
			this.advance();
18
		}
19
		
20
		public Parser(string src) {
21
			this.scanner = Scanner.scanner(src).GetEnumerator();
22
			this.advance();
23
		}
24
		
25
		private void advance() {
26
			if(this.scanner.MoveNext()) {
27
				ArrayList current = (ArrayList) this.scanner.Current;
28
				this.t = (string) current[0];
29
				this.v = current[1];
30
			} else {
31
				this.t = null;
32
				this.v = null;
33
			}
34
		}
35
		
36
		private object expect(string t) {
37
			if(this.t != t) {
38
				throw(new Exception("SyntaxError: " + t + " excepted"));
39
			}
40
			object v = this.v;
41
			advance();
42
			return v;
43
		}
44
		
45
		private Node node(string name) {
46
			Node node = new Node(name);
47
			while(t == "(") {
48
				advance();
49
				name = (string) expect("name");
50
				if(t == "value") {
51
					node[name] = expect("value");
52
					expect(")");
53
				} else if(t == "(") {
54
					node.Append(this.node(name));
55
				} else if(t == ")") {
56
					node.Append(new Node(name));
57
					advance();
58
				}
59
			}
60
			expect(")");
61
			return node;
62
		}
63
		
64
		public Node parse() {
65
			expect("(");
66
			string name = (string) expect("name");
67
			return this.node(name);
68
		}
69
	}
70
}

Up to file-list aml/scanner.cs:

1
using System;
2
using System.Collections;
3
using System.Text.RegularExpressions;
4
5
namespace aml.scanner {
6
	
7
	class Scanner {
8
		
9
		public static IEnumerable scanner(string src) {
10
			Regex re = new Regex("([-+]?\\d+)|(\\w+)|\"([^\"]*)\"|'([^']*)'|([()])|#.*?\n|\\s+");
11
			foreach (Match match in re.Matches(src)) {
12
				int i = 0;
13
				foreach(Group group in match.Groups) {
14
					if (group.Success && i > 0) {
15
						ArrayList token = new ArrayList(2);
16
						if (i == 1) {
17
							token.Add("value");
18
							token.Add(Convert.ToInt32(group.Value));
19
							yield return token;
20
						} else if (i == 2) {
21
							token.Add("name");
22
							token.Add(group.Value);
23
							yield return token;
24
						} else if (i == 3 || i == 4) {
25
							token.Add("value");
26
							token.Add(group.Value);
27
							yield return token;
28
						} else if (i == 5) {
29
							token.Add(group.Value);
30
							token.Add(group.Value);
31
							yield return token;
32
						}
33
					}
34
					i++;
35
				}
36
			}
37
		}
38
	}
39
}

Up to file-list aml/validator.cs:

1
using System;
2
using System.Collections;
3
using System.Text.RegularExpressions;
4
5
namespace aml.validator {
6
	
7
	public class Type {
8
		
9
		private Regex regex;
10
		
11
		public Type(Regex regex) {
12
			this.regex = regex;
13
		}
14
		
15
		public bool validate(string source) {
16
			return this.regex.IsMatch(source);
17
		}
18
	}
19
	
20
	public class Node {
21
		
22
		public string name;
23
		public Hashtable attributes = new Hashtable();
24
		public ArrayList children = new ArrayList();
25
		
26
		public Node(string name) {
27
			this.name = name;
28
		}
29
	}
30
	
31
	public class Validator {
32
		
33
		private Node document;
34
		private Node definition;
35
		
36
		public Validator(Node document, Node definition) {
37
			this.document = document;
38
			this.definition = definition;
39
		}
40
		
41
		public bool validate() {
42
			return true;
43
		}
44
	}	
45
}