gonum.org/v1/gonum@v0.14.0/graph/formats/rdf/parse_actions.rl (about)

     1  // Copyright ©2020 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  %%{
     6  	machine nquads;
     7  
     8  	action StartSubject {
     9  		subject = p
    10  	}
    11  
    12  	action StartPredicate {
    13  		predicate = p
    14  	}
    15  
    16  	action StartObject {
    17  		object = p
    18  	}
    19  
    20  	action StartLabel {
    21  		label = p
    22  	}
    23  
    24  	action StartIRI {
    25  		iri = p
    26  	}
    27  
    28  	action SetSubject {
    29  		if subject < 0 {
    30  			panic("unexpected parser state: subject start not set")
    31  		}
    32  		s.Subject.Value = string(data[subject:p])
    33  	}
    34  
    35  	action SetPredicate {
    36  		if predicate < 0 {
    37  			panic("unexpected parser state: predicate start not set")
    38  		}
    39  		s.Predicate.Value = string(data[predicate:p])
    40  	}
    41  
    42  	action SetObject {
    43  		if object < 0 {
    44  			panic("unexpected parser state: object start not set")
    45  		}
    46  		s.Object.Value = string(data[object:p])
    47  	}
    48  
    49  	action SetLabel {
    50  		if label < 0 {
    51  			panic("unexpected parser state: label start not set")
    52  		}
    53  		s.Label.Value = string(data[label:p])
    54  	}
    55  
    56  	action EndIRI {
    57  		if iri < 0 {
    58  			panic("unexpected parser state: iri start not set")
    59  		}
    60  		switch u, err := url.Parse(string(data[iri:p])); {
    61  		case err != nil:
    62  			return s, err
    63  		case !u.IsAbs():
    64  			return s, fmt.Errorf("%w: relative IRI ref %q", ErrInvalid, string(data[iri:p]))
    65  		}
    66  	}
    67  
    68  	action Return {
    69  		return s, nil
    70  	}
    71  
    72  	action Comment {
    73  	}
    74  
    75  	action Error {
    76  		if p < len(data) {
    77  			if r := data[p]; r < unicode.MaxASCII {
    78  				return s, fmt.Errorf("%w: unexpected rune %q at %d", ErrInvalid, data[p], p)
    79  			} else {
    80  				return s, fmt.Errorf("%w: unexpected rune %q (\\u%04[2]x) at %d", ErrInvalid, data[p], p)
    81  			}
    82  		}
    83  		return s, ErrIncomplete
    84  	}
    85  }%%