gonum.org/v1/gonum@v0.14.0/graph/formats/rdf/extract_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 extract;
     7  
     8  	action StartIRI {
     9  		iri = p
    10  	}
    11  
    12  	action EndIRI {
    13  		if iri < 0 {
    14  			panic("unexpected parser state: iri start not set")
    15  		}
    16  		iriText = unEscape(data[iri:p])
    17  		if kind == Invalid {
    18  			kind = IRI
    19  		}
    20  	}
    21  
    22  	action StartBlank {
    23  		blank = p
    24  	}
    25  
    26  	action EndBlank {
    27  		if blank < 0 {
    28  			panic("unexpected parser state: blank start not set")
    29  		}
    30  		blankText = string(data[blank:p])
    31  		kind = Blank
    32  	}
    33  
    34  	action StartLiteral {
    35  		literal = p
    36  	}
    37  
    38  	action EndLiteral {
    39  		if literal < 0 {
    40  			panic("unexpected parser state: literal start not set")
    41  		}
    42  		literalText = unEscape(data[literal:p])
    43  		kind = Literal
    44  	}
    45  
    46  	action StartLang {
    47  		lang = p
    48  	}
    49  
    50  	action EndLang {
    51  		if lang < 0 {
    52  			panic("unexpected parser state: lang start not set")
    53  		}
    54  		langText = string(data[lang:p])
    55  	}
    56  
    57  	action Return {
    58  		switch kind {
    59  		case IRI:
    60  			return iriText, "", kind, nil
    61  		case Blank:
    62  			return blankText, "", kind, nil
    63  		case Literal:
    64  			qual = iriText
    65  			if qual == "" {
    66  				qual = langText
    67  			}
    68  			return literalText, qual, kind, nil
    69  		default:
    70  			return "", "", kind, ErrInvalidTerm
    71  		}
    72  	}
    73  
    74  	action Error {
    75  		if p < len(data) {
    76  			if r := data[p]; r < unicode.MaxASCII {
    77  				return "", "", Invalid, fmt.Errorf("%w: unexpected rune %q at %d", ErrInvalidTerm, data[p], p)
    78  			} else {
    79  				return "", "", Invalid, fmt.Errorf("%w: unexpected rune %q (\\u%04[2]x) at %d", ErrInvalidTerm, data[p], p)
    80  			}
    81  		}
    82  		return "", "", Invalid, ErrIncompleteTerm
    83  	}
    84  }%%