BiggerRadius

parser test

Dec 31st, 2024 (edited)
55
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.63 KB | None | 0 0
  1. grammar VarDeclare {
  2.     token TOP { <type> <array>? \s+ <name> \s* <value>? }
  3.     proto token type {*}
  4.     token type:sym<int>    { 'int' }
  5.     token type:sym<float>  { 'float' }
  6.     token type:sym<string> { 'string' }
  7.     token array { '[]' }
  8.     token name { <[A..Za..z]>+ }
  9.     proto token value {*}
  10.     token value:sym<int>    { '=' \s* <[0..9]>+ }
  11.     token value:sym<float>  { '=' \s* <[0..9]>* '.' <[0..9]>+ }
  12.     token value:sym<string> { '=' \s* '"' <[A..Za..z]>+ '"' }
  13. }
  14.  
  15. class Variable {
  16.     has Str $.type is required;
  17.     has Str $.name is required;
  18.     has $.value;
  19.     has @.arrayValue;
  20.     has Bool $.isArray is rw = False;
  21.  
  22.     method declareAsArr() {
  23.         $.isArray = True;
  24.     }
  25.  
  26.     multi method new(Str $type, Str $name, $value) {
  27.         self.bless(:$type, :$name, :$value);
  28.     }
  29.  
  30.     multi method new(Str $type, Str $name, *@arrayValue) {
  31.         self.bless(:$type, :$name, :@arrayValue);
  32.         self.declareAsArr();
  33.     }
  34.  
  35.     method gist(Variable:D:){
  36.         if $.isArray {
  37.             return "$.type[] $.name = " ~ @.arrayValue.join(', ');
  38.         } else {
  39.             return "$.type $.name = " ~ ($.value // 'undefined');
  40.         }
  41.     }
  42. }
  43.  
  44. sub parseCode(Str $input) {
  45.     my $parsed = VarDeclare.parse($input);
  46.     if $parsed {
  47.         my $type = $parsed<type>.Str;
  48.         my $name = $parsed<name>.Str;
  49.         my $isArray = $parsed<array>:exists;
  50.         my $valueMatch = $parsed<value>;
  51.         if $isArray {
  52.             my @values = $valueMatch ?? $valueMatch.Str.subst('=', '').split(',').map(*.trim) !! ();
  53.                 =begin pod
  54.                 say $type;
  55.                 say $name;
  56.                 say $isArray;
  57.                 say @values;
  58.                 =end pod
  59.                 return Variable.new($type, $name, |@values);
  60.         } else {
  61.             my $value = $valueMatch ?? $valueMatch.Str.subst('=', '').trim !! Nil;
  62.                 =begin pod
  63.                 say $type;
  64.                 say $name;
  65.                 say $isArray;
  66.                 say $value;
  67.                 =end pod
  68.                 return Variable.new($type, $name, $value);
  69.         }
  70.     } else {
  71.         say "Parsing failed for input: $input";
  72.         return Nil;
  73.     }
  74. }
  75.  
  76. #TODO make arrays work with multiple value declaration
  77.  
  78. #=begin pod
  79.  
  80. my $input = "int a = 100";
  81. my $output1 = parseCode($input);
  82. my $input2 = 'string[] animal = "cat,dog"';
  83. my $output2 = parseCode($input2);
  84. my $input3 = "float Hb";
  85. my $output3 = parseCode($input3);
  86. my $input4 = 'string hhh = "hope"';
  87. my $output4 = parseCode($input4);
  88.  
  89. say $output1;
  90. say $output2;
  91. say $output3;
  92. say $output4;
  93. #=end pod
  94.  
Advertisement
Comments
  • BiggerRadius
    267 days
    # Perl 6 0.14 KB | 0 0
    1. currently output would be
    2. Parsing failed for input: string[] animal = "cat,dog"
    3. int a = 100
    4. (Any)
    5. float Hb = undefined
    6. string hhh = "hope"
Add Comment
Please, Sign In to add comment