forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayerIdentifier.yy
146 lines (121 loc) · 3.84 KB
/
layerIdentifier.yy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
%code requires {
struct Sdf_LayerIdentifierParserContext;
}
%{
#include "pxr/usd/sdf/layer.h"
#include <string>
#define YYSTYPE std::string
struct yy_buffer_state;
typedef void *yyscan_t;
//--------------------------------------------------------------------
// Context object for storing parser results
//--------------------------------------------------------------------
struct Sdf_LayerIdentifierParserContext
{
std::string layerPath;
SdfLayer::FileFormatArguments args;
std::string error;
yyscan_t scanner;
};
//--------------------------------------------------------------------
// Extern declarations to scanner data and functions
//--------------------------------------------------------------------
// Generated bison symbols.
int layerIdentifierYyparse(Sdf_LayerIdentifierParserContext *);
int layerIdentifierYylex(YYSTYPE *, yyscan_t);
int layerIdentifierYylex_init(yyscan_t *);
int layerIdentifierYylex_destroy(yyscan_t);
yy_buffer_state *layerIdentifierYy_scan_string(const char*, yyscan_t);
void layerIdentifierYy_delete_buffer(yy_buffer_state *, yyscan_t);
static void layerIdentifierYyerror(
Sdf_LayerIdentifierParserContext *, const char *);
#define yyscanner context->scanner
%}
// Make this re-entrant
%define api.pure
%lex-param { yyscan_t yyscanner }
%parse-param { Sdf_LayerIdentifierParserContext *context }
%token TOK_LAYER_PATH
%token TOK_IDENTIFIER
%token TOK_VALUE
%%
identifier:
layer_path
| layer_path '?' arguments
;
layer_path:
TOK_LAYER_PATH {
context->layerPath = $1;
}
arguments:
argument
| argument '&' arguments
;
argument:
TOK_IDENTIFIER '=' TOK_VALUE {
context->args[$1] = $3;
}
| TOK_IDENTIFIER '=' TOK_IDENTIFIER {
context->args[$1] = $3;
}
| TOK_IDENTIFIER '=' {
context->args[$1] = std::string();
}
;
%%
static void
layerIdentifierYyerror(
Sdf_LayerIdentifierParserContext *context, const char *msg)
{
context->error = msg;
}
bool
Sdf_ParseLayerIdentifier(
const std::string& argumentString,
std::string* layerPath,
SdfLayer::FileFormatArguments* args)
{
Sdf_LayerIdentifierParserContext context;
// Initialize the scanner, allowing it to be reentrant.
layerIdentifierYylex_init(&context.scanner);
// Run parser.
yy_buffer_state *buf = layerIdentifierYy_scan_string(
argumentString.c_str(), context.scanner);
bool success = false;
if (layerIdentifierYyparse(&context) == 0) {
success = true;
layerPath->swap(context.layerPath);
args->swap(context.args);
}
else {
success = false;
TF_CODING_ERROR(context.error);
}
// Clean up.
layerIdentifierYy_delete_buffer(buf, context.scanner);
layerIdentifierYylex_destroy(context.scanner);
return success;
}