Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.array;
- import std.conv;
- import pegged.grammar;
- mixin(grammar(`
- CompDefGrammar:
- DefinitionSet <- Declaration*
- Declaration <- "component" Spacing NewType Spacing Definition Spacing
- Definition <- :'{' Spacing ValueDef* Spacing :'}'
- NewType <- ComponentName Spacing :':' Spacing ComponentId
- ComponentName <- identifier
- ComponentId <- identifier / digit*
- ValueDef <- Spacing ((Type TypeSize) / Type) Spacing Identifier Spacing
- Identifier <- identifier
- Type <- "double" / "float" / "int" / "char"
- TypeSize <- :'[' digit* :']'
- Spacing <- :(' ' / '\t' / endOfLine)*
- `));
- struct ValueDefinition
- {
- string TypeName;
- string Name;
- int Size;
- }
- class Component
- {
- public:
- bool AddValue(string typeName, string name, int size = 0)
- {
- foreach(v; Values)
- {
- if(v.Name == name)
- return false;
- }
- ValueDefinition vd;
- vd.TypeName = typeName;
- vd.Name = name;
- vd.Size = size;
- Values ~= vd;
- return true;
- }
- string Name;
- string Id;
- ValueDefinition[] Values;
- }
- void ParseComponentDefinitions(ParseTree p, ref Component[] outComponents)
- {
- switch(p.name)
- {
- case "CompDefGrammar.DefinitionSet":
- foreach(np; p.children)
- ParseComponentDefinitions(np, outComponents);
- break;
- case "CompDefGrammar.Declaration":
- if(p.children.length != 2)
- writeln("Invalid component definition.");
- else
- {
- foreach(np; p.children)
- ParseComponentDefinitions(np, outComponents);
- }
- break;
- case "CompDefGrammar.NewType":
- if(p.matches.length != 2)
- writeln("Invalid component type definition.");
- else
- {
- Component ncomp = new Component();
- ncomp.Name = p.matches[0];
- ncomp.Id = p.matches[1];
- outComponents ~= ncomp;
- }
- break;
- case "CompDefGrammar.ValueDef":
- if(p.matches.length < 2)
- writeln("Invalid component value definition.");
- else
- {
- Component ccomp = outComponents[$-1];
- bool hasSizeProp = p.matches.length == 3;
- ccomp.AddValue(p.matches[0], hasSizeProp ? p.matches[2] : p.matches[1], hasSizeProp ? to!int(p.matches[1]) : 0);
- }
- break;
- default:
- foreach(np; p.children)
- ParseComponentDefinitions(np, outComponents);
- }
- }
- void main(string[] cmdLineArgs)
- {
- foreach(string c; cmdLineArgs)
- writeln(c);
- string testContent = r"component Name : Id
- {
- double x
- int nx
- char[5] rs
- char n
- }
- component dst : 5
- {
- double x
- int nx
- char rs
- int[5] n
- }
- ";
- auto p = CompDefGrammar(testContent);
- if((p.children.length >= 1) && (p.children[0].children.length >= 1))
- {
- Component[] parsedComponents;
- writeln("Parsed ", p.children[0].children.length, " component definitions");
- ParseComponentDefinitions(p.children[0], parsedComponents);
- writeln("Writing ", parsedComponents.length, " components to ");
- foreach(c; parsedComponents)
- writeln(c.Name, " ", c.Id, " ", c.Values);
- }
- else
- {
- writeln("Errors occurred while parsing the specified file.");
- }
- writeln(testContent);
- writeln(p);
- writeln(p.matches);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement