Blame src/yang.l

Packit 8fb591
/**
Packit 8fb591
 * @file yang.l
Packit 8fb591
 * @author Pavol Vican
Packit 8fb591
 * @brief YANG parser for libyang (flex grammar)
Packit 8fb591
 *
Packit 8fb591
 * Copyright (c) 2015 CESNET, z.s.p.o.
Packit 8fb591
 *
Packit 8fb591
 * This source code is licensed under BSD 3-Clause License (the "License").
Packit 8fb591
 * You may not use this file except in compliance with the License.
Packit 8fb591
 * You may obtain a copy of the License at
Packit 8fb591
 *
Packit 8fb591
 *     https://opensource.org/licenses/BSD-3-Clause
Packit 8fb591
 */
Packit 8fb591

Packit 8fb591
%option noyywrap nounput noinput reentrant bison-bridge bison-locations
Packit 8fb591

Packit 8fb591
%{
Packit 8fb591
#include "parser_yang.h"
Packit 8fb591
#include "parser_yang_bis.h"
Packit 8fb591

Packit 8fb591
#define YY_USER_ACTION yylloc->first_column = yylloc->last_column +1;\
Packit 8fb591
                       yylloc->last_column = yylloc->first_column + yyleng - 1;
Packit 8fb591
%}
Packit 8fb591

Packit 8fb591
U       [\x80-\xbf]
Packit 8fb591
U2     [\xc2-\xdf]{U}
Packit 8fb591
U3     [\xe0][\xa0-\xbf]{U}|[\xe1-\xec]{U}{U}|[\xed][\x80-\x9f]{U}|[\xee-\xef]{U}{U}
Packit 8fb591
U4     [\xf0][\x90-\xbf]{U}{U}|[\xf1-\xf3]{U}{U}{U}|[\xf4][\x80-\x8f]{U}{U}
Packit 8fb591

Packit 8fb591
%x COMMENT1
Packit 8fb591
%x COMMENT2
Packit 8fb591
%x PATH
Packit 8fb591
%x DOUBLEQUOTES
Packit 8fb591
%x SINGLEQUOTES
Packit 8fb591

Packit 8fb591
%%
Packit 8fb591
 int tab_count = 0;
Packit 8fb591
 int size_str = 0;
Packit 8fb591
 int column = 0;
Packit 8fb591
 char *str = NULL;
Packit 8fb591
 int _state = YY_START;
Packit 8fb591
 int i;
Packit 8fb591
 uint32_t value;
Packit 8fb591

Packit 8fb591

Packit 8fb591
"/*" {_state = YY_START; BEGIN COMMENT1; }
Packit 8fb591
<COMMENT1,COMMENT2>[\x00-\x09\x0B-\x7f]|{U2}|{U3}|{U4}
Packit 8fb591
<COMMENT1>\n {yylloc->last_column = 0;}
Packit 8fb591
<COMMENT1>"*/" {BEGIN _state; }
Packit 8fb591
"//" {_state = YY_START; BEGIN COMMENT2;}
Packit 8fb591
<COMMENT2>\n {BEGIN _state; yylloc->last_column = 0; return EOL; }
Packit 8fb591
"anyxml" { return ANYXML_KEYWORD; }
Packit 8fb591
"argument" { return ARGUMENT_KEYWORD; }
Packit 8fb591
"augment" { BEGIN PATH; return AUGMENT_KEYWORD; }
Packit 8fb591
"base" { return BASE_KEYWORD; }
Packit 8fb591
"belongs-to" { return BELONGS_TO_KEYWORD; }
Packit 8fb591
"bit" { return BIT_KEYWORD; }
Packit 8fb591
"case" { return CASE_KEYWORD; }
Packit 8fb591
"choice" { return CHOICE_KEYWORD; }
Packit 8fb591
"config" { return CONFIG_KEYWORD; }
Packit 8fb591
"contact" { return CONTACT_KEYWORD; }
Packit 8fb591
"container" { return CONTAINER_KEYWORD; }
Packit 8fb591
"default" { return DEFAULT_KEYWORD; }
Packit 8fb591
"description" { return DESCRIPTION_KEYWORD; }
Packit 8fb591
"enum" { return ENUM_KEYWORD; }
Packit 8fb591
"error-app-tag" { return ERROR_APP_TAG_KEYWORD; }
Packit 8fb591
"error-message" { return ERROR_MESSAGE_KEYWORD; }
Packit 8fb591
"extension" { return EXTENSION_KEYWORD; }
Packit 8fb591
"deviation" { BEGIN PATH; return DEVIATION_KEYWORD; }
Packit 8fb591
"deviate" { return DEVIATE_KEYWORD; }
Packit 8fb591
"feature" { return FEATURE_KEYWORD; }
Packit 8fb591
"fraction-digits" { return FRACTION_DIGITS_KEYWORD; }
Packit 8fb591
"grouping" { return GROUPING_KEYWORD; }
Packit 8fb591
"identity" { return IDENTITY_KEYWORD; }
Packit 8fb591
"if-feature" { return IF_FEATURE_KEYWORD; }
Packit 8fb591
"import" { return IMPORT_KEYWORD; }
Packit 8fb591
"include" { return INCLUDE_KEYWORD; }
Packit 8fb591
"input" { return INPUT_KEYWORD; }
Packit 8fb591
"key" { return KEY_KEYWORD; }
Packit 8fb591
"leaf" { return LEAF_KEYWORD; }
Packit 8fb591
"leaf-list" { return LEAF_LIST_KEYWORD; }
Packit 8fb591
"length" { return LENGTH_KEYWORD; }
Packit 8fb591
"list" { return LIST_KEYWORD; }
Packit 8fb591
"mandatory" { return MANDATORY_KEYWORD; }
Packit 8fb591
"max-elements" { return MAX_ELEMENTS_KEYWORD; }
Packit 8fb591
"min-elements" { return MIN_ELEMENTS_KEYWORD; }
Packit 8fb591
"module" { return MODULE_KEYWORD; }
Packit 8fb591
"must" { return MUST_KEYWORD; }
Packit 8fb591
"namespace" { return NAMESPACE_KEYWORD; }
Packit 8fb591
"notification" { return NOTIFICATION_KEYWORD; }
Packit 8fb591
"ordered-by" { return ORDERED_BY_KEYWORD; }
Packit 8fb591
"organization" { return ORGANIZATION_KEYWORD; }
Packit 8fb591
"output" { return OUTPUT_KEYWORD; }
Packit 8fb591
"path" { BEGIN PATH; return PATH_KEYWORD; }
Packit 8fb591
"pattern" { return PATTERN_KEYWORD; }
Packit 8fb591
"position" { return POSITION_KEYWORD; }
Packit 8fb591
"prefix" { return PREFIX_KEYWORD; }
Packit 8fb591
"presence" { return PRESENCE_KEYWORD; }
Packit 8fb591
"range" { return RANGE_KEYWORD; }
Packit 8fb591
"reference" { return REFERENCE_KEYWORD; }
Packit 8fb591
"refine" { BEGIN PATH; return REFINE_KEYWORD; }
Packit 8fb591
"require-instance" { return REQUIRE_INSTANCE_KEYWORD; }
Packit 8fb591
"revision" { return REVISION_KEYWORD; }
Packit 8fb591
"revision-date" { return REVISION_DATE_KEYWORD; }
Packit 8fb591
"rpc" { return RPC_KEYWORD; }
Packit 8fb591
"status" { return STATUS_KEYWORD; }
Packit 8fb591
"submodule" { return (yylloc->last_line) ? SUBMODULE_EXT_KEYWORD : SUBMODULE_KEYWORD; }
Packit 8fb591
"type" { return TYPE_KEYWORD; }
Packit 8fb591
"typedef" { return TYPEDEF_KEYWORD; }
Packit 8fb591
"unique" { BEGIN PATH; return UNIQUE_KEYWORD; }
Packit 8fb591
"units" { return UNITS_KEYWORD; }
Packit 8fb591
"uses" { return USES_KEYWORD; }
Packit 8fb591
"value" { return VALUE_KEYWORD; }
Packit 8fb591
"when" { return WHEN_KEYWORD; }
Packit 8fb591
"yang-version" { return YANG_VERSION_KEYWORD; }
Packit 8fb591
"yin-element" { return YIN_ELEMENT_KEYWORD; }
Packit 8fb591
"add" { return ADD_KEYWORD; }
Packit 8fb591
"current" { return CURRENT_KEYWORD; }
Packit 8fb591
<PATH>"current" { return CURRENT_KEYWORD; }
Packit 8fb591
"delete" { return DELETE_KEYWORD; }
Packit 8fb591
"deprecated" { return DEPRECATED_KEYWORD; }
Packit 8fb591
"false" { return FALSE_KEYWORD; }
Packit 8fb591
"not-supported" { return NOT_SUPPORTED_KEYWORD; }
Packit 8fb591
"obsolete" { return OBSOLETE_KEYWORD; }
Packit 8fb591
"replace" { return REPLACE_KEYWORD; }
Packit 8fb591
"system" { return SYSTEM_KEYWORD; }
Packit 8fb591
"true" { return TRUE_KEYWORD; }
Packit 8fb591
"unbounded" { return UNBOUNDED_KEYWORD; }
Packit 8fb591
"user" { return USER_KEYWORD; }
Packit 8fb591
"action" {return ACTION_KEYWORD; }
Packit 8fb591
"modifier" {return MODIFIER_KEYWORD; }
Packit 8fb591
"anydata" {return ANYDATA_KEYWORD; }
Packit 8fb591
"{" |
Packit 8fb591
"}" |
Packit 8fb591
";" |
Packit 8fb591
"+"  { return yytext[0];}  /* unsolved problem with concatenate string '+' */
Packit 8fb591
"\"" {_state = YY_START; BEGIN DOUBLEQUOTES; str = yytext; column = yylloc->first_column; }
Packit 8fb591
<DOUBLEQUOTES>\t|\\t { tab_count++; size_str += yyleng; }
Packit 8fb591
<DOUBLEQUOTES>[\x0D\x20-\x21\x23-\x5b\x5d-\x7f]|{U2} { size_str += yyleng; }
Packit 8fb591
<DOUBLEQUOTES>\\([\x09\x0A\x0D\x20-\x7f]|{U2}|{U3}|{U4}) { size_str += yyleng; }
Packit 8fb591
<DOUBLEQUOTES,SINGLEQUOTES>\n {yylloc->last_column = 0; size_str++; }
Packit 8fb591
<DOUBLEQUOTES,SINGLEQUOTES>{U3} {
Packit 8fb591
    value = ((uint32_t)(yytext[0] & 0xf) << 12) | ((uint32_t)(yytext[1] & 0x3f) << 6) | (yytext[2] & 0x3f);
Packit 8fb591
    if (((value & 0xf800) == 0xd800) ||
Packit 8fb591
        (value >= 0xfdd0 && value <= 0xfdef) ||
Packit 8fb591
        (value & 0xffe) == 0xffe) {
Packit 8fb591
        /* exclude surrogate blocks %xD800-DFFF */
Packit 8fb591
        /* exclude noncharacters %xFDD0-FDEF */
Packit 8fb591
        /* exclude noncharacters %xFFFE-FFFF */
Packit 8fb591
        LOGVAL(yyget_extra(yyscanner), LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid UTF-8 value 0x%08x", value);
Packit 8fb591
        yylloc->first_line = -1;
Packit 8fb591
        return ERROR;
Packit 8fb591
    }
Packit 8fb591
    size_str += 3;
Packit 8fb591
}
Packit 8fb591
<DOUBLEQUOTES,SINGLEQUOTES>{U4} {
Packit 8fb591
    value = ((uint32_t)(yytext[0] & 0x7) << 18) | ((uint32_t)(yytext[1] & 0x3f) << 12) | ((uint32_t)(yytext[2] & 0x3f) << 6) | (yytext[3] & 0x3f);
Packit 8fb591
    if ((value & 0xffe) == 0xffe) {
Packit 8fb591
        /* exclude noncharacters %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
Packit 8fb591
         * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
Packit 8fb591
         * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
Packit 8fb591
        LOGVAL(yyget_extra(yyscanner), LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid UTF-8 value 0x%08x", value);
Packit 8fb591
        yylloc->first_line = -1;
Packit 8fb591
        return ERROR;
Packit 8fb591
    }
Packit 8fb591
    size_str += 4;
Packit 8fb591
}
Packit 8fb591
<DOUBLEQUOTES>\" { yylval->i = tab_count;
Packit 8fb591
                   BEGIN _state;
Packit 8fb591
                   yytext = str;
Packit 8fb591
                   yyleng = size_str + 2;
Packit 8fb591
                   yylloc->first_column = column;
Packit 8fb591
                   return STRING;
Packit 8fb591
                 }
Packit 8fb591
<DOUBLEQUOTES><<EOF>> { LOGVAL(yyget_extra(yyscanner), LYE_SPEC, LY_VLOG_NONE, NULL, "Unterminated double-quoted string.");
Packit 8fb591
                        yylloc->first_line = -1;
Packit 8fb591
                        return ERROR;
Packit 8fb591
                      }
Packit 8fb591
<INITIAL,PATH>"'" { _state = YY_START;
Packit 8fb591
                    BEGIN SINGLEQUOTES;
Packit 8fb591
                    str = yytext;
Packit 8fb591
                    column = yylloc->first_column;
Packit 8fb591
                  }
Packit 8fb591
<SINGLEQUOTES>[\x09\x0D\x20-\x26\x28-\x7f]|{U2} { size_str += yyleng; }
Packit 8fb591
<SINGLEQUOTES>"'" { BEGIN _state;
Packit 8fb591
                    yytext = str;
Packit 8fb591
                    yyleng = size_str + 2;
Packit 8fb591
                    yylloc->first_column = column;
Packit 8fb591
                    return STRING;
Packit 8fb591
                  }
Packit 8fb591
<SINGLEQUOTES><<EOF>> { LOGVAL(yyget_extra(yyscanner), LYE_SPEC, LY_VLOG_NONE, NULL, "Unterminated single-quoted string.");
Packit 8fb591
                        yylloc->first_line = -1;
Packit 8fb591
                        return ERROR;
Packit 8fb591
                      }
Packit 8fb591
"0" {return ZERO;}
Packit 8fb591
"-"[1-9][0-9]* {return INTEGER;}
Packit 8fb591
[1-9][0-9]* {return NON_NEGATIVE_INTEGER;}
Packit 8fb591
<PATH>"/" |
Packit 8fb591
<PATH>"[" |
Packit 8fb591
<PATH>"]" |
Packit 8fb591
<PATH>"=" |
Packit 8fb591
<PATH>"(" |
Packit 8fb591
<PATH>")" |
Packit 8fb591
<PATH>"+" {return yytext[0];}
Packit 8fb591
<PATH>".." {return DOUBLEDOT;}
Packit 8fb591
<PATH>\n  |
Packit 8fb591
<PATH>\r\n { yylloc->last_column = 0; return EOL;}
Packit 8fb591
<PATH>[ \t]+ { return WHITESPACE;}
Packit 8fb591
<PATH>";" |
Packit 8fb591
<PATH>"{" {BEGIN INITIAL; return yytext[0];}
Packit 8fb591
<PATH>"\"" {_state = YY_START; BEGIN DOUBLEQUOTES; str = yytext; column = yylloc->first_column; }
Packit 8fb591
<PATH>"//" {_state = YY_START; BEGIN COMMENT2;}
Packit 8fb591
<PATH>"/*" {_state = YY_START; BEGIN COMMENT1;}
Packit 8fb591
<PATH>[A-Za-z_][-A-Za-z0-9_\.]* {return IDENTIFIER;}
Packit 8fb591
<PATH>[A-Za-z_][-A-Za-z0-9_\.]*:[A-Za-z_][-A-Za-z0-9_\.]*   {return IDENTIFIERPREFIX;}
Packit 8fb591
[0-9]{4}[-][0-9]{2}[-][0-9]{2} {return REVISION_DATE;}
Packit 8fb591
[A-Za-z_][-A-Za-z0-9_\.]* {return IDENTIFIER;}
Packit 8fb591
[A-Za-z_][-A-Za-z0-9_\.]*:[A-Za-z_][-A-Za-z0-9_\.]*   {return IDENTIFIERPREFIX;}
Packit 8fb591
([\x21\x23-\x26\x28-\x3a\x3c-\x7a\x7c\x7e-\x7f]|{U2}|{U3}|{U4})+ {
Packit 8fb591
    if (yytext[0] == '/' && yytext[1] == '/') {
Packit 8fb591
        _state = YY_START;
Packit 8fb591
        yyless(2);
Packit 8fb591
        BEGIN COMMENT2;
Packit 8fb591
    } else if (yytext[0] == '/' && yytext[1] == '*') {
Packit 8fb591
        _state = YY_START;
Packit 8fb591
        yyless(2);
Packit 8fb591
        BEGIN COMMENT1;
Packit 8fb591
    } else {
Packit 8fb591
        i = 0;
Packit 8fb591
        while (i < yyleng) {
Packit 8fb591
            if (!(yytext[i] & 0x80)) {
Packit 8fb591
                /* one byte character */
Packit 8fb591
                if (yytext[i] == '/') {
Packit 8fb591
                    if (yytext[i + 1] == '/') {
Packit 8fb591
                        yyless(i);
Packit 8fb591
                        return STRINGS;
Packit 8fb591
                    } else if (yytext[i + 1] == '*') {
Packit 8fb591
                        yyless(i);
Packit 8fb591
                        return STRINGS;
Packit 8fb591
                    }
Packit 8fb591
                } else if (yytext[i] == '*' && yytext[i + 1] == '/') {
Packit 8fb591
                    if (!i) {
Packit 8fb591
                        yyless(1);
Packit 8fb591
                        return ERROR;
Packit 8fb591
                    } else {
Packit 8fb591
                        yyless(i);
Packit 8fb591
                        return STRINGS;
Packit 8fb591
                    }
Packit 8fb591
                }
Packit 8fb591
                ++i;
Packit 8fb591
            } else if (!(yytext[i] & 0x20)) {
Packit 8fb591
                /* two bytes character */
Packit 8fb591
                i += 2;
Packit 8fb591
            } else if (!(yytext[i] & 0x10)) {
Packit 8fb591
                /* three bytes character */
Packit 8fb591
                value = ((uint32_t)(yytext[i] & 0xf) << 12) | ((uint32_t)(yytext[i + 1] & 0x3f) << 6) | (yytext[i + 2] & 0x3f);
Packit 8fb591
                if (((value & 0xf800) == 0xd800) ||
Packit 8fb591
                    (value >= 0xfdd0 && value <= 0xfdef) ||
Packit 8fb591
                    (value & 0xffe) == 0xffe) {
Packit 8fb591
                    /* exclude surrogate blocks %xD800-DFFF */
Packit 8fb591
                    /* exclude noncharacters %xFDD0-FDEF */
Packit 8fb591
                    /* exclude noncharacters %xFFFE-FFFF */
Packit 8fb591
                    LOGVAL(yyget_extra(yyscanner), LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid UTF-8 value 0x%08x", value);
Packit 8fb591
                    yylloc->first_line = -1;
Packit 8fb591
                    return ERROR;
Packit 8fb591
                }
Packit 8fb591
                i += 3;
Packit 8fb591
            } else {
Packit 8fb591
                /* four bytes character */
Packit 8fb591
                value = ((uint32_t)(yytext[i] & 0x7) << 18) | ((uint32_t)(yytext[i + 1] & 0x3f) << 12) | ((uint32_t)(yytext[i + 2] & 0x3f) << 6) | (yytext[i + 3] & 0x3f);
Packit 8fb591
                if ((value & 0xffe) == 0xffe) {
Packit 8fb591
                    /* exclude noncharacters %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
Packit 8fb591
                     * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
Packit 8fb591
                     * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
Packit 8fb591
                    LOGVAL(yyget_extra(yyscanner), LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid UTF-8 value 0x%08x", value);
Packit 8fb591
                    yylloc->first_line = -1;
Packit 8fb591
                    return ERROR;
Packit 8fb591
                }
Packit 8fb591
                i += 4;
Packit 8fb591
            }
Packit 8fb591
        }
Packit 8fb591
        return STRINGS;
Packit 8fb591
    }
Packit 8fb591
}
Packit 8fb591
\n |
Packit 8fb591
\r\n { yylloc->last_column = 0; return EOL; }
Packit 8fb591
[ \t]+ { return WHITESPACE;}
Packit 8fb591

Packit 8fb591
<COMMENT1,COMMENT2,DOUBLEQUOTES,SINGLEQUOTES,INITIAL,PATH>[\x00-\xff] {
Packit 8fb591
    LOGVAL(yyget_extra(yyscanner), LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid UTF-8 leading byte 0x%02x", yytext[0]);
Packit 8fb591
    yylloc->first_line = -1;
Packit 8fb591
    return ERROR;
Packit 8fb591
}
Packit 8fb591

Packit 8fb591
%%