05.11., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 6b700714 authored by Eckhart Arnold's avatar Eckhart Arnold

examples/yaml: stub added

parent badf8f9a
# yaml
PLACE A SHORT DESCRIPTION HERE
Author: AUTHOR'S NAME <EMAIL>, AFFILIATION
## License
yaml is open source software under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0)
Copyright YEAR AUTHOR'S NAME <EMAIL>, AFFILIATION
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Life is but a walking shadow
// json-example
{
"object": { "one": 1, "two": 2, "three": ["3"] },
"array": ["one", 2, 3],
"string": "string example",
"true": true,
"false": false,
"null": null
}
#!/usr/bin/python3
"""tst_yaml_grammar.py - runs the unit tests for the yaml-grammar
"""
import os
import sys
LOGGING = False
sys.path.append(r'/home/eckhart/Entwicklung/DHParser')
scriptpath = os.path.dirname(__file__)
try:
from DHParser import dsl
import DHParser.log
from DHParser import testing
except ModuleNotFoundError:
print('Could not import DHParser. Please adjust sys.path in file '
'"%s" manually' % __file__)
sys.exit(1)
def recompile_grammar(grammar_src, force):
grammar_tests_dir = os.path.join(scriptpath, 'grammar_tests')
testing.create_test_templates(grammar_src, grammar_tests_dir)
with DHParser.log.logging(False):
# recompiles Grammar only if it has changed
if not dsl.recompile_grammar(grammar_src, force=force,
notify=lambda: print('recompiling ' + grammar_src)):
print('\nErrors while recompiling "%s":' % grammar_src +
'\n--------------------------------------\n\n')
with open('yaml_ebnf_ERRORS.txt') as f:
print(f.read())
sys.exit(1)
def run_grammar_tests(glob_pattern, get_grammar, get_transformer):
with DHParser.log.logging(LOGGING):
error_report = testing.grammar_suite(
os.path.join(scriptpath, 'grammar_tests'),
get_grammar, get_transformer,
fn_patterns=[glob_pattern], report=True, verbose=True)
return error_report
if __name__ == '__main__':
argv = sys.argv[:]
if len(argv) > 1 and sys.argv[1] == "--debug":
LOGGING = True
del argv[1]
if (len(argv) >= 2 and (argv[1].endswith('.ebnf') or
os.path.splitext(argv[1])[1].lower() in testing.TEST_READERS.keys())):
# if called with a single filename that is either an EBNF file or a known
# test file type then use the given argument
arg = argv[1]
else:
# otherwise run all tests in the test directory
arg = '*_test_*.ini'
if arg.endswith('.ebnf'):
recompile_grammar(arg, force=True)
else:
recompile_grammar(os.path.join(scriptpath, 'yaml.ebnf'),
force=False)
sys.path.append('.')
from yamlCompiler import get_grammar, get_transformer
error_report = run_grammar_tests(arg, get_grammar, get_transformer)
if error_report:
print('\n')
print(error_report)
sys.exit(1)
print('ready.\n')
# yaml-grammar see: https://yaml.org/spec/1.2/spec.html
#######################################################################
#
# EBNF-Directives
#
#######################################################################
@ whitespace = vertical # implicit whitespace, includes any number of line feeds
@ literalws = right # literals have implicit whitespace on the right hand side
@ comment = /(?:\/\/|#).*/ # comments range from a '#' odr '//' sign to the end of the line
@ ignorecase = False # literals and regular expressions are case-sensitive
@ drop = whitespace, token # drop tokens and whitespace
#######################################################################
#
#: JSON elements
#
#######################################################################
json = ~ element EOF
element = value
value = object | array | string | number | bool | null
object = "{" [member { "," member }] "}"
member = string ":" element
array = "[" [value { "," value }] "]"
string = `"` CHARACTERS `"` ~
number = INT FRAC EXP ~
bool = /true/~ | /false/~ # use regexes so values are not dropped as tokens
null = "null"
#######################################################################
#
#: JSON primitives
#
#######################################################################
CHARACTERS = { /[^"\\]+/ | ESCAPE }
ESCAPE = /\\[\/bnrt\\]/ | /\\u/ HEX HEX HEX HEX
HEX = /[0-9a-fA-F]/
INT = [`-`] /[0-9]/ | /[1-9][0-9]+/
FRAC = [ `.` /[0-9]+/ ]
EXP = [ (`E`|`e`) [`+`|`-`] /[0-9]+/ ]
EOF = !/./ # no more characters ahead, end of file reached
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment