github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/config/expr.y (about)

     1  // This is the yacc input for creating the parser for interpolation
     2  // expressions in Go.
     3  
     4  // To build it:
     5  //
     6  // go tool yacc -p "expr" expr.y (produces y.go)
     7  //
     8  
     9  %{
    10  package config
    11  
    12  import (
    13  	"fmt"
    14  )
    15  
    16  %}
    17  
    18  %union {
    19  	expr Interpolation
    20      str string
    21  	variable InterpolatedVariable
    22  	args []Interpolation
    23  }
    24  
    25  %type	<args> args
    26  %type   <expr> expr
    27  %type   <str> string
    28  %type   <variable> variable
    29  
    30  %token  <str> STRING IDENTIFIER
    31  %token	<str> COMMA LEFTPAREN RIGHTPAREN
    32  
    33  %%
    34  
    35  top:
    36  	expr
    37  	{
    38  		exprResult = $1
    39  	}
    40  
    41  expr:
    42  	string
    43  	{
    44  		$$ = &LiteralInterpolation{Literal: $1}
    45  	}
    46  |	variable
    47  	{
    48  		$$ = &VariableInterpolation{Variable: $1}
    49  	}
    50  |	IDENTIFIER LEFTPAREN args RIGHTPAREN
    51  	{
    52  		f, ok := Funcs[$1]
    53  		if !ok {
    54  			exprErrors = append(exprErrors, fmt.Errorf(
    55  				"Unknown function: %s", $1))
    56  		}
    57  
    58  		$$ = &FunctionInterpolation{Func: f, Args: $3}
    59  	}
    60  
    61  args:
    62  	{
    63  		$$ = nil
    64  	}
    65  |	args COMMA expr
    66  	{
    67  		$$ = append($1, $3)
    68  	}
    69  |	expr
    70  	{
    71  		$$ = append($$, $1)
    72  	}
    73  
    74  string:
    75  	STRING
    76  	{
    77  		$$ = $1
    78  	}
    79  
    80  variable:
    81  	IDENTIFIER
    82  	{
    83  		var err error
    84  		$$, err = NewInterpolatedVariable($1)
    85  		if err != nil {
    86  			exprErrors = append(exprErrors, fmt.Errorf(
    87  				"Error parsing variable '%s': %s", $1, err))
    88  		}
    89  	}
    90  
    91  %%