github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/tao/auth/scan.go (about) 1 // Copyright (c) 2014, Kevin Walsh. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package auth 16 17 // This file implements Scan() functions for all elements so they can be used 18 // with fmt.Scanf() and friends. 19 20 import ( 21 "fmt" 22 ) 23 24 // Scan parses a Prin, with optional outer parens. 25 func (p *Prin) Scan(state fmt.ScanState, verb rune) error { 26 parser := newParser(state) 27 prin, err := parser.parsePrin() 28 if err != nil { 29 return err 30 } 31 *p = prin 32 return nil 33 } 34 35 // Scan parses a PrinTail, with optional outer parens. 36 func (pt *PrinTail) Scan(state fmt.ScanState, verb rune) error { 37 parser := newParser(state) 38 prinTail, err := parser.parsePrinTail() 39 if err != nil { 40 return err 41 } 42 *pt = prinTail 43 return nil 44 } 45 46 // Scan parses a PrinExt. 47 func (e *PrinExt) Scan(state fmt.ScanState, verb rune) error { 48 parser := newParser(state) 49 name, args, err := parser.expectNameAndArgs() 50 if err != nil { 51 return err 52 } 53 e.Name = name 54 e.Arg = args 55 return nil 56 } 57 58 // Scan parses a SubPrin. 59 func (e *SubPrin) Scan(state fmt.ScanState, verb rune) error { 60 parser := newParser(state) 61 subprin, err := parser.expectSubPrin() 62 if err != nil { 63 return err 64 } 65 *e = subprin 66 return nil 67 } 68 69 // AnyTerm is a struct that can be used in when scanning for a Term, since Term 70 // itself is an interface and interface pointers are not valid receivers. 71 // TODO(kwalsh) Can this be accomplished with a pointer to interface? 72 type AnyTerm struct { 73 Term Term 74 } 75 76 // Scan parses a Term, with optional outer parens. 77 func (t *AnyTerm) Scan(state fmt.ScanState, verb rune) error { 78 parser := newParser(state) 79 term, err := parser.parseTerm() 80 if err != nil { 81 return err 82 } 83 t.Term = term 84 return nil 85 } 86 87 // Scan parses a Str, with optional outer parens. 88 func (t *Str) Scan(state fmt.ScanState, verb rune) error { 89 parser := newParser(state) 90 s, err := parser.parseStr() 91 if err != nil { 92 return err 93 } 94 *t = s 95 return nil 96 } 97 98 // Scan parses a Bytes, with optional outer parens. 99 func (t *Bytes) Scan(state fmt.ScanState, verb rune) error { 100 parser := newParser(state) 101 b, err := parser.parseBytes() 102 if err != nil { 103 return err 104 } 105 *t = b 106 return nil 107 } 108 109 // Scan parses an Int, with optional outer parens. 110 func (t *Int) Scan(state fmt.ScanState, verb rune) error { 111 parser := newParser(state) 112 i, err := parser.parseInt() 113 if err != nil { 114 return err 115 } 116 *t = i 117 return nil 118 } 119 120 // Scan parses a TermVar, with optional outer parens. 121 func (t *TermVar) Scan(state fmt.ScanState, verb rune) error { 122 parser := newParser(state) 123 v, err := parser.parseTermVar() 124 if err != nil { 125 return err 126 } 127 *t = v 128 return nil 129 } 130 131 // AnyForm is a struct that can be used in when scanning for a Form, since Form 132 // itself is an interface and interface pointers are not valid receivers. 133 // TODO(kwalsh) Can this be accomplished with a pointer to interface? 134 type AnyForm struct { 135 Form Form 136 } 137 138 // Scan parses a Form, with optional outer parens. This function is not greedy: 139 // it consumes only as much input as necessary to obtain a valid formula. For 140 // example, "(p says a and b ...)" and "p says (a and b ...) will be parsed in 141 // their entirety, but given "p says a and b ... ", only "p says a" will be 142 // parsed. 143 func (f *AnyForm) Scan(state fmt.ScanState, verb rune) error { 144 parser := newParser(state) 145 form, err := parser.parseForm(false) 146 if err != nil { 147 return err 148 } 149 f.Form = form 150 return nil 151 } 152 153 // Scan parses a Pred, with optional outer parens. 154 func (f *Pred) Scan(state fmt.ScanState, verb rune) error { 155 parser := newParser(state) 156 pred, err := parser.parsePred() 157 if err != nil { 158 return err 159 } 160 *f = pred 161 return nil 162 } 163 164 // Scan parses a Const, with optional outer parens. This function is not greedy. 165 func (f *Const) Scan(state fmt.ScanState, verb rune) error { 166 parser := newParser(state) 167 c, err := parser.parseConst() 168 if err != nil { 169 return err 170 } 171 *f = c 172 return nil 173 } 174 175 // Scan parses a Not, with optional outer parens. This function is not greedy. 176 func (f *Not) Scan(state fmt.ScanState, verb rune) error { 177 parser := newParser(state) 178 form, err := parser.parseForm(false) 179 if err != nil { 180 return err 181 } 182 n, ok := form.(Not) 183 if !ok { 184 return fmt.Errorf(`expecting "not": %s`, form) 185 } 186 *f = n 187 return nil 188 } 189 190 // Scan parses an And, with required outer parens. This function is not greedy. 191 func (f *And) Scan(state fmt.ScanState, verb rune) error { 192 parser := newParser(state) 193 form, err := parser.parseForm(false) 194 if err != nil { 195 return err 196 } 197 n, ok := form.(And) 198 if ok { 199 *f = n 200 return nil 201 } 202 err = parser.expect(tokenAnd) 203 if err != nil { 204 return err 205 } 206 m, err := parser.parseForm(false) 207 *f = And{Conjunct: []Form{n, m}} 208 return nil 209 } 210 211 // Scan parses an Or, with required outer parens. This function is not greedy. 212 func (f *Or) Scan(state fmt.ScanState, verb rune) error { 213 parser := newParser(state) 214 form, err := parser.parseForm(false) 215 if err != nil { 216 return err 217 } 218 n, ok := form.(Or) 219 if ok { 220 *f = n 221 return nil 222 } 223 err = parser.expect(tokenOr) 224 if err != nil { 225 return err 226 } 227 m, err := parser.parseForm(false) 228 *f = Or{Disjunct: []Form{n, m}} 229 return nil 230 } 231 232 // Scan parses an Implies, with required outer parens. This function is not 233 // greedy. 234 func (f *Implies) Scan(state fmt.ScanState, verb rune) error { 235 parser := newParser(state) 236 form, err := parser.parseForm(false) 237 if err != nil { 238 return err 239 } 240 n, ok := form.(Implies) 241 if ok { 242 *f = n 243 return nil 244 } 245 err = parser.expect(tokenImplies) 246 if err != nil { 247 return err 248 } 249 m, err := parser.parseForm(false) 250 *f = Implies{n, m} 251 return nil 252 } 253 254 // Scan parses a Says, with optional outer parens. This function is not greedy. 255 func (f *Says) Scan(state fmt.ScanState, verb rune) error { 256 parser := newParser(state) 257 form, err := parser.parseForm(false) 258 if err != nil { 259 return err 260 } 261 n, ok := form.(Says) 262 if !ok { 263 return fmt.Errorf(`expecting "says": %s`, form) 264 } 265 *f = n 266 return nil 267 } 268 269 // Scan parses a Speaksfor, with optional outer parens. This function is not 270 // greedy. 271 func (f *Speaksfor) Scan(state fmt.ScanState, verb rune) error { 272 parser := newParser(state) 273 form, err := parser.parseForm(false) 274 if err != nil { 275 return err 276 } 277 n, ok := form.(Speaksfor) 278 if !ok { 279 return fmt.Errorf(`expecting "speaksfor": %s`, form) 280 } 281 *f = n 282 return nil 283 } 284 285 // Scan parses a Forall, with optional outer parens. This function is not greedy. 286 func (f *Forall) Scan(state fmt.ScanState, verb rune) error { 287 parser := newParser(state) 288 form, err := parser.parseForm(false) 289 if err != nil { 290 return err 291 } 292 n, ok := form.(Forall) 293 if !ok { 294 return fmt.Errorf(`expecting "forall": %s`, form) 295 } 296 *f = n 297 return nil 298 } 299 300 // Scan parses an Exists, with optional outer parens. This function is not greedy. 301 func (f *Exists) Scan(state fmt.ScanState, verb rune) error { 302 parser := newParser(state) 303 form, err := parser.parseForm(false) 304 if err != nil { 305 return err 306 } 307 n, ok := form.(Exists) 308 if !ok { 309 return fmt.Errorf(`expecting "exists": %s`, form) 310 } 311 *f = n 312 return nil 313 }