Advertisement
Guest User

Untitled

a guest
Dec 7th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.07 KB | None | 0 0
  1. import std.stdio;
  2. import std.array;
  3. import std.conv;
  4. import pegged.grammar;
  5.  
  6. mixin(grammar(`
  7.     CompDefGrammar:
  8.         DefinitionSet <- Declaration*
  9.         Declaration <- "component" Spacing NewType Spacing Definition Spacing
  10.         Definition <- :'{' Spacing ValueDef* Spacing :'}'
  11.  
  12.         NewType <- ComponentName Spacing :':' Spacing ComponentId
  13.         ComponentName <- identifier
  14.         ComponentId <- identifier / digit*
  15.         ValueDef <- Spacing ((Type TypeSize) / Type) Spacing Identifier Spacing
  16.  
  17.         Identifier <- identifier
  18.         Type <- "double" / "float" / "int" / "char"
  19.         TypeSize <- :'[' digit* :']'
  20.         Spacing <- :(' ' / '\t' / endOfLine)*
  21.     `));
  22.  
  23. struct ValueDefinition
  24. {
  25.     string TypeName;
  26.     string Name;
  27.     int Size;
  28. }
  29.  
  30. class Component
  31. {
  32. public:
  33.     bool AddValue(string typeName, string name, int size = 0)
  34.     {
  35.         foreach(v; Values)
  36.         {
  37.             if(v.Name == name)
  38.                 return false;
  39.         }
  40.  
  41.         ValueDefinition vd;
  42.         vd.TypeName = typeName;
  43.         vd.Name = name;
  44.         vd.Size = size;
  45.         Values ~= vd;
  46.  
  47.         return true;
  48.     }
  49.  
  50.     string Name;
  51.     string Id;
  52.     ValueDefinition[] Values;
  53. }
  54.  
  55. void ParseComponentDefinitions(ParseTree p, ref Component[] outComponents)
  56. {
  57.     switch(p.name)
  58.     {
  59.         case "CompDefGrammar.DefinitionSet":
  60.             foreach(np; p.children)
  61.                 ParseComponentDefinitions(np, outComponents);
  62.         break;
  63.         case "CompDefGrammar.Declaration":
  64.             if(p.children.length != 2)
  65.                 writeln("Invalid component definition.");
  66.             else
  67.             {
  68.                 foreach(np; p.children)
  69.                     ParseComponentDefinitions(np, outComponents);
  70.             }
  71.         break;
  72.         case "CompDefGrammar.NewType":
  73.             if(p.matches.length != 2)
  74.                 writeln("Invalid component type definition.");
  75.             else
  76.             {
  77.                 Component ncomp = new Component();
  78.                 ncomp.Name = p.matches[0];
  79.                 ncomp.Id = p.matches[1];
  80.  
  81.                 outComponents ~= ncomp;
  82.             }
  83.         break;
  84.         case "CompDefGrammar.ValueDef":
  85.             if(p.matches.length < 2)
  86.                 writeln("Invalid component value definition.");
  87.             else
  88.             {
  89.                 Component ccomp = outComponents[$-1];
  90.                 bool hasSizeProp = p.matches.length == 3;
  91.                 ccomp.AddValue(p.matches[0], hasSizeProp ? p.matches[2] : p.matches[1], hasSizeProp ? to!int(p.matches[1]) : 0);
  92.             }
  93.         break;
  94.         default:
  95.             foreach(np; p.children)
  96.                 ParseComponentDefinitions(np, outComponents);
  97.     }
  98. }
  99.  
  100. void main(string[] cmdLineArgs)
  101. {
  102.     foreach(string c; cmdLineArgs)
  103.         writeln(c);
  104.  
  105.     string testContent = r"component Name : Id
  106.         {
  107.             double x
  108.             int nx
  109.             char[5] rs
  110.             char n
  111.             }
  112. component dst : 5
  113.         {
  114.             double x
  115.             int nx
  116.             char rs
  117.             int[5] n
  118.             }
  119.             ";
  120.  
  121.     auto p = CompDefGrammar(testContent);
  122.  
  123.     if((p.children.length >= 1) && (p.children[0].children.length >= 1))
  124.     {
  125.         Component[] parsedComponents;
  126.         writeln("Parsed ", p.children[0].children.length, " component definitions");
  127.         ParseComponentDefinitions(p.children[0], parsedComponents);
  128.  
  129.         writeln("Writing ", parsedComponents.length, " components to ");
  130.  
  131.         foreach(c; parsedComponents)
  132.             writeln(c.Name, " ", c.Id, " ", c.Values);
  133.     }
  134.     else
  135.     {
  136.         writeln("Errors occurred while parsing the specified file.");
  137.     }
  138.     writeln(testContent);
  139.     writeln(p);
  140.     writeln(p.matches);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement