kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/verifier/assertions.yy (about)

     1  // Started from the calc++ example code as part of the Bison-3.0 distribution.
     2  %skeleton "lalr1.cc"
     3  %defines
     4  %define parser_class_name {AssertionParserImpl}
     5  %{
     6  /*
     7   * Copyright 2014 The Kythe Authors. All rights reserved.
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   */
    21  #include <string>
    22  #include "kythe/cxx/verifier/assertion_ast.h"
    23  #ifdef yylex
    24  #undef yylex
    25  #endif
    26  #define yylex kythe::verifier::AssertionParser::lex
    27  namespace kythe {
    28  namespace verifier {
    29  class AssertionParser;
    30  }
    31  }
    32  %}
    33  %parse-param { ::kythe::verifier::AssertionParser &parser_context }
    34  %lex-param { ::kythe::verifier::AssertionParser &parser_context }
    35  %locations
    36  %initial-action
    37  {
    38    @$.initialize(&parser_context.file());
    39    @$.begin.column = 1;
    40    @$.end.column = 1;
    41  };
    42  %define parse.trace
    43  %{
    44  #include "kythe/cxx/verifier/assertions.h"
    45  #define newAst new (parser_context.arena_) kythe::verifier::
    46  %}
    47  %token
    48    END 0 "end of file"
    49    LPAREN  "("
    50    RPAREN  ")"
    51    LBRACE  "{"
    52    RBRACE  "}"
    53    BANG    "!"
    54    COMMA   ","
    55    DONTCARE "_"
    56    APOSTROPHE "'"
    57    AT "@"
    58    AT_HAT "@^"
    59    AT_CASH "@$"
    60    DOT "."
    61    WHAT "?"
    62    EQUALS "="
    63    COLON ":"
    64    PLUS "+"
    65  ;
    66  %token <string> IDENTIFIER "identifier"
    67  %token <string> STRING "string"
    68  %token <string> NUMBER "number"
    69  %token <string> HASH_NUMBER "hashnumber"
    70  %type <node> exp
    71  %type <node> atom
    72  %type <node> goal
    73  %type <node> exp_tuple_star
    74  %type <int_> nested_goals
    75  %type <string> string_or_identifier
    76  %type <int_> location_spec
    77  %type <int_> location_spec_hash
    78  %type <size_t_> exp_tuple_plus
    79  %define parse.error verbose
    80  %%
    81  %start unit;
    82  unit: goals  { };
    83  
    84  goals:
    85    /* empty */ {}
    86  | goals goal { parser_context.AppendGoal(parser_context.group_id(), $2); }
    87  | goals LBRACE { parser_context.EnterGoalGroup(@2, false); }
    88      nested_goals RBRACE { parser_context.ExitGoalGroup(@5); }
    89  | goals BANG LBRACE { parser_context.EnterGoalGroup(@2, true); }
    90      nested_goals RBRACE { parser_context.ExitGoalGroup(@5); }
    91  
    92  nested_goals:
    93    nested_goals goal {
    94      parser_context.AppendGoal(parser_context.group_id(), $2);
    95      $$ = 0;
    96    }
    97  | goal { parser_context.AppendGoal(parser_context.group_id(), $1); $$ = 0; }
    98  
    99  string_or_identifier:
   100    "identifier" { $$ = $1; }
   101  | "string" { $$ = $1; }
   102  
   103  goal:
   104    exp string_or_identifier exp {
   105      $$ = parser_context.CreateSimpleEdgeFact(@1 + @3, $1, $2, $3, nullptr);
   106    }
   107  | exp "." string_or_identifier exp {
   108      $$ = parser_context.CreateSimpleNodeFact(@1 + @4, $1, $3, $4);
   109    }
   110  | exp string_or_identifier "." atom exp {
   111      $$ = parser_context.CreateSimpleEdgeFact(@1 + @5, $1, $2, $5, $4);
   112    }
   113  
   114  exp:
   115    atom exp_tuple_star { $$ = newAst App($1, $2); };
   116  | atom { $$ = $1; };
   117  | atom "=" exp {
   118      parser_context.AppendGoal(
   119          parser_context.group_id(),
   120          parser_context.CreateEqualityConstraint(@2, $1, $3)
   121      );
   122      $$ = $1;
   123    };
   124  
   125  atom:
   126      "identifier"       { $$ = parser_context.CreateAtom(@1, $1); }
   127    | "string"           { $$ = parser_context.CreateIdentifier(@1, $1); }
   128    | "_"                { $$ = parser_context.CreateDontCare(@1); }
   129    | "number"           { $$ = parser_context.CreateIdentifier(@1, $1); };
   130    | "@" location_spec_hash  { $$ = parser_context.CreateAnchorSpec(@1); };
   131    | "@^" location_spec_hash {
   132        $$ = parser_context.CreateOffsetSpec(@1, false);
   133      };
   134    | "@$" location_spec_hash {
   135        $$ = parser_context.CreateOffsetSpec(@1, true);
   136      };
   137    | "identifier" "?" {
   138        $$ = parser_context.CreateInspect(
   139            @2,
   140            $1,
   141            parser_context.CreateAtom(@1, $1)
   142        );
   143      }
   144    | "_" "?" {
   145        $$ = parser_context.CreateInspect(
   146            @2,
   147            "_",
   148            parser_context.CreateDontCare(@1)
   149        );
   150      }
   151  
   152  exp_tuple_plus:
   153      exp_tuple_plus "," exp { parser_context.PushNode($3); $$ = $1 + 1; }
   154    | exp { parser_context.PushNode($1); $$ = 1; }
   155  
   156  exp_tuple_star:
   157      "(" ")" { $$ = newAst Tuple(@1, 0, nullptr); }
   158    | "(" exp_tuple_plus ")" {
   159      $$ = newAst Tuple(@1, $2, parser_context.PopNodes($2));
   160    }
   161  
   162  location_spec:
   163      string_or_identifier { parser_context.PushLocationSpec($1); $$ = 0; }
   164    | ":" "number" string_or_identifier {
   165      parser_context.PushAbsoluteLocationSpec($3, $2); $$ = 0;
   166    }
   167    | "+" "number" string_or_identifier {
   168      parser_context.PushRelativeLocationSpec($3, $2); $$ = 0;
   169    }
   170  
   171  location_spec_hash:
   172      location_spec { $$ = $1; }
   173    | HASH_NUMBER location_spec {
   174      parser_context.SetTopLocationSpecMatchNumber($1); $$ = $2;
   175    }
   176  
   177  %%
   178  void yy::AssertionParserImpl::error(const location_type &l,
   179                                      const std::string &m) {
   180    parser_context.Error(l, m);
   181  }