[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
How to operate on peg:tree structure
From: |
Malte Frank Gerdes |
Subject: |
How to operate on peg:tree structure |
Date: |
Thu, 20 Feb 2020 23:29:41 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) |
Thanks for your grammar :).
I extended the grammar and it now is able to parse all *.bp files i have
tested from the android10_r52 source tree!
That means i now have to do something useful with the peg tree. How does
it work? I don'tknow exactly what to do from here. I guess i have to walk
the tree somehow and execute the rules as i go, but i don't have a clue
how to do this :/.
The current program looks like this:
(use-modules (ice-9 peg)
(ice-9 pretty-print)
(ice-9 rdelim))
(define *bp*
(read-delimited "" (open-input-file (cadr (command-line))) 'concat))
(define-peg-string-patterns
"blueprint <-- (comment* (ws? defvar ws?)* module* comment*)* .*
module <-- rule ws? map ws?
rule <-- 'cc_binary_host' / 'cc_test_library' / 'cc_test_host' /
'cc_binary' / 'cc_test' /'cc_library_host_static' /
'cc_library_static' / 'cc_library_shared' /
'cc_library_headers' / 'cc_library' / 'cc_defaults' /
'cc_benchmark' / 'python_test_host' / 'genrule' / 'filegroup' /
'ndk_headers' / 'ndk_library' / 'llndk_library' /
'python_binary_host' / 'cc_prebuilt_binary' / 'prebuilt_etc' /
'python_defaults' / 'phony'
defvar <-- var ws? eq ws? value
eq < '='
nl < '\n'
ws < (' ' / '\t' / nl)+
comma < ','
colon < ':'
maplb < '{'
maprb < '}'
listlb < '['
listrb < ']'
comment < ws? '//' ( . !nl )* . ws?
strb < '\"'
append <-- '+'
key <-- ws? (comment ws?)* [-a-zA-Z0-9_]+
bool <-- 'true' / 'false'
integer <-- '-'? [0-9]+
string <-- strb (. !strb)* . strb
map <-- maplb ws? maprb / maplb ws? attribute (comma comment* ws?
attribute)* comma? ws? maprb
attribute <-- key ws? colon ws? expr
expr <-- (value ws? append ws? &value)* value
value <-- bool / integer / string / map / list / var
var <-- [-a-zA-Z0-9_]+
list <-- listlb ws? listrb / listlb ws? comment* ws? value (comma
comment* ws? value)* comma? ws? comment* listrb
")
(pretty-print (peg:tree (match-pattern blueprint *bp*)))
And a *very long* parse tree is here:
(blueprint
((module
(rule "cc_defaults")
(map (attribute
(key "name")
(expr (value (string "adb_defaults"))))
((attribute
(key "cflags")
(expr (value (list (value (string "-Wall"))
((value (string "-Wextra"))
(value (string "-Werror"))
(value (string "-Wexit-time-destructors"))
(value (string "-Wno-unused-parameter"))
(value (string
"-Wno-missing-field-initializers"))
(value (string "-Wthread-safety"))
(value (string "-Wvla"))
(value (string "-DADB_HOST=1"))
(value (string "-DALLOW_ADBD_ROOT=0")))))))
(attribute
(key "cpp_std")
(expr (value (string "experimental"))))
(attribute
(key "use_version_lib")
(expr (value (bool "true"))))
(attribute
(key "compile_multilib")
(expr (value (string "first"))))
(attribute
(key "target")
(expr (value (map (attribute
(key "darwin")
(expr (value (map (attribute
(key "host_ldlibs")
(expr (value (list (value
(string
"-lpthread"))
((value
(string
"-framework CoreFoundation"))
(value
(string
"-framework IOKit"))
(value
(string
"-lobjc")))))))))))
(attribute
(key "windows")
(expr (value (map (attribute
(key "cflags")
(expr (value (list (value
(string
"-DUNICODE=1"))
((value
(string
"-D_UNICODE=1"))
(value
(string
"-D_GNU_SOURCE"))
(value
(string
"-D_POSIX_SOURCE"))
(value
(string
"-Wno-thread-safety")))))))
(attribute
(key "host_ldlibs")
(expr (value (list (value
(string
"-lws2_32"))
((value
(string
"-lgdi32"))
(value
(string
"-luserenv"))))))))))))))))))
(module
(rule "cc_defaults")
(map (attribute
(key "name")
(expr (value (string "adbd_defaults"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adb_defaults"))))))
(attribute
(key "cflags")
(expr (value (list (value (string "-UADB_HOST"))
(value (string "-DADB_HOST=0"))))))
(attribute
(key "product_variables")
(expr (value (map (attribute
(key "debuggable")
(expr (value (map (attribute
(key "cflags")
(expr (value (list (value
(string
"-UALLOW_ADBD_ROOT"))
((value
(string
"-DALLOW_ADBD_ROOT=1"))
(value
(string
"-DALLOW_ADBD_DISABLE_VERITY"))
(value
(string
"-DALLOW_ADBD_NO_AUTH"))))))))))))))))))
(module
(rule "cc_defaults")
(map (attribute
(key "name")
(expr (value (string "host_adbd_supported"))))
((attribute
(key "host_supported")
(expr (value (bool "true"))))
(attribute
(key "target")
(expr (value (map (attribute
(key "linux")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"true"))))
(attribute
(key "host_ldlibs")
(expr (value (list (value
(string
"-lresolv"))
(value
(string
"-lutil"))))))))))
((attribute
(key "darwin")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"false"))))))))
(attribute
(key "windows")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"false")))))))))))))))))
(((defvar
(var "libadb_srcs")
(value (list (value (string "adb.cpp"))
((value (string "adb_io.cpp"))
(value (string "adb_listeners.cpp"))
(value (string "adb_trace.cpp"))
(value (string "adb_unique_fd.cpp"))
(value (string "adb_utils.cpp"))
(value (string "fdevent.cpp"))
(value (string "services.cpp"))
(value (string "sockets.cpp"))
(value (string "socket_spec.cpp"))
(value (string "sysdeps/errno.cpp"))
(value (string "transport.cpp"))
(value (string "transport_fd.cpp"))
(value (string "transport_local.cpp"))
(value (string "transport_usb.cpp"))))))
(defvar
(var "libadb_posix_srcs")
(value (list (value (string "sysdeps_unix.cpp"))
(value (string "sysdeps/posix/network.cpp")))))
(defvar
(var "libadb_test_srcs")
(value (list (value (string "adb_io_test.cpp"))
((value (string "adb_listeners_test.cpp"))
(value (string "adb_utils_test.cpp"))
(value (string "fdevent_test.cpp"))
(value (string "socket_spec_test.cpp"))
(value (string "socket_test.cpp"))
(value (string "sysdeps_test.cpp"))
(value (string "sysdeps/stat_test.cpp"))
(value (string "transport_test.cpp"))
(value (string "types_test.cpp")))))))
((module
(rule "cc_library_host_static")
(map (attribute
(key "name")
(expr (value (string "libadb_host"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adb_defaults"))))))
(attribute
(key "srcs")
(expr ((value (var "libadb_srcs")) (append "+"))
(value (list (value (string "client/auth.cpp"))
((value (string "client/usb_libusb.cpp"))
(value (string "client/usb_dispatch.cpp"))
(value (string
"client/transport_mdns.cpp")))))))
(attribute
(key "generated_headers")
(expr (value (list (value (string "platform_tools_version"))))))
(attribute
(key "target")
(expr (value (map (attribute
(key "linux")
(expr (value (map (attribute
(key "srcs")
(expr (value (list (value
(string
"client/usb_linux.cpp"))))))))))
((attribute
(key "darwin")
(expr (value (map (attribute
(key "srcs")
(expr (value (list
(value (string
"client/usb_osx.cpp"))))))))))
(attribute
(key "not_windows")
(expr (value (map (attribute
(key "srcs")
(expr (value (var
"libadb_posix_srcs"))))))))
(attribute
(key "windows")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"true"))))
((attribute
(key "srcs")
(expr (value (list
(value (string
"client/usb_windows.cpp"))
((value (string
"sysdeps_win32.cpp"))
(value (string
"sysdeps/win32/errno.cpp"))
(value (string
"sysdeps/win32/stat.cpp")))))))
(attribute
(key "shared_libs")
(expr (value (list
(value (string
"AdbWinApi"))))))))))))))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libbase"))
((value (string "libcrypto_utils"))
(value (string "libcrypto"))
(value (string "libdiagnose_usb"))
(value (string "libmdnssd"))
(value (string "libusb"))
(value (string "libutils"))
(value (string "liblog"))
(value (string "libcutils"))))))))))
(module
(rule "cc_test_host")
(map (attribute
(key "name")
(expr (value (string "adb_test"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adb_defaults"))))))
(attribute
(key "srcs")
(expr (value (var "libadb_test_srcs"))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libadb_host"))
((value (string "libbase"))
(value (string "libcutils"))
(value (string "libcrypto_utils"))
(value (string "libcrypto"))
(value (string "libmdnssd"))
(value (string "libdiagnose_usb"))
(value (string "libusb")))))))
(attribute
(key "target")
(expr (value (map (attribute
(key "windows")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"true"))))
(attribute
(key "shared_libs")
(expr (value (list (value
(string
"AdbWinApi")))))))))))))))))
(module
(rule "cc_benchmark")
(map (attribute
(key "name")
(expr (value (string "adb_benchmark"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adb_defaults"))))))
(attribute
(key "srcs")
(expr (value (list (value (string "transport_benchmark.cpp"))))))
(attribute
(key "target")
(expr (value (map (attribute
(key "android")
(expr (value (map (attribute
(key "static_libs")
(expr (value (list (value
(string
"libadbd"))))))))))
(attribute
(key "host")
(expr (value (map (attribute
(key "static_libs")
(expr (value (list (value
(string
"libadb_host"))))))))))))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libbase"))
((value (string "libcutils"))
(value (string "libcrypto_utils"))
(value (string "libcrypto"))
(value (string "libdiagnose_usb"))
(value (string "liblog"))
(value (string "libusb"))))))))))
(module
(rule "cc_binary_host")
(map (attribute
(key "name")
(expr (value (string "adb"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adb_defaults"))))))
(attribute
(key "srcs")
(expr (value (list (value (string "client/adb_client.cpp"))
((value (string "client/bugreport.cpp"))
(value (string "client/commandline.cpp"))
(value (string
"client/file_sync_client.cpp"))
(value (string "client/main.cpp"))
(value (string "client/console.cpp"))
(value (string "client/adb_install.cpp"))
(value (string "client/line_printer.cpp"))
(value (string
"shell_service_protocol.cpp")))))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libadb_host"))
((value (string "libbase"))
(value (string "libcutils"))
(value (string "libcrypto_utils"))
(value (string "libcrypto"))
(value (string "libdiagnose_usb"))
(value (string "liblog"))
(value (string "libmdnssd"))
(value (string "libusb"))
(value (string "libutils"))
(value (string "liblog"))
(value (string "libcutils")))))))
(attribute
(key "stl")
(expr (value (string "libc++_static"))))
(attribute
(key "shared_libs")
(expr (value list)))
(attribute
(key "required")
(expr (value (list (value (string "deploypatchgenerator"))))))
(attribute
(key "dist")
(expr (value (map (attribute
(key "targets")
(expr (value (list (value (string
"dist_files"))
((value (string "sdk"))
(value (string
"win_sdk")))))))))))
(attribute
(key "target")
(expr (value (map (attribute
(key "darwin")
(expr (value (map (attribute
(key "cflags")
(expr (value (list (value
(string
"-Wno-sizeof-pointer-memaccess"))))))))))
(attribute
(key "windows")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"true"))))
((attribute
(key "ldflags")
(expr (value (list
(value (string
"-municode"))))))
(attribute
(key "shared_libs")
(expr (value (list
(value (string
"AdbWinApi"))))))
(attribute
(key "required")
(expr (value (list
(value (string
"AdbWinUsbApi"))))))))))))))))))))
((module
(rule "cc_library_static")
(map (attribute
(key "name")
(expr (value (string "libadbd_core"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adbd_defaults"))
(value (string "host_adbd_supported"))))))
(attribute
(key "recovery_available")
(expr (value (bool "true"))))
(attribute
(key "compile_multilib")
(expr (value (string "both"))))
(attribute
(key "srcs")
(expr (((value (var "libadb_srcs")) (append "+"))
((value (var "libadb_posix_srcs")) (append "+")))
(value (list (value (string "daemon/auth.cpp"))
(value (string "daemon/jdwp_service.cpp"))))))
(attribute
(key "local_include_dirs")
(expr (value (list (value (string "daemon/include"))))))
(attribute
(key "generated_headers")
(expr (value (list (value (string "platform_tools_version"))))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libdiagnose_usb"))))))
(attribute
(key "shared_libs")
(expr (value (list (value (string "libasyncio"))
((value (string "libbase"))
(value (string "libcrypto"))
(value (string "libcrypto_utils"))
(value (string "libcutils"))
(value (string "liblog")))))))
(attribute
(key "target")
(expr (value (map (attribute
(key "android")
(expr (value (map (attribute
(key "whole_static_libs")
(expr (value (list (value
(string
"libqemu_pipe"))))))
(attribute
(key "srcs")
(expr (value (list (value
(string
"daemon/transport_qemu.cpp"))
((value
(string
"daemon/usb.cpp"))
(value
(string
"daemon/usb_ffs.cpp"))
(value
(string
"daemon/usb_legacy.cpp")))))))))))
(attribute
(key "linux_glibc")
(expr (value (map (attribute
(key "srcs")
(expr (value (list (value
(string
"daemon/usb_dummy.cpp")))))))))))))))))
(module
(rule "cc_library")
(map (attribute
(key "name")
(expr (value (string "libadbd_services"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adbd_defaults"))
(value (string "host_adbd_supported"))))))
(attribute
(key "recovery_available")
(expr (value (bool "true"))))
(attribute
(key "compile_multilib")
(expr (value (string "both"))))
(attribute
(key "srcs")
(expr (value (list (value (string "daemon/file_sync_service.cpp"))
((value (string "daemon/services.cpp"))
(value (string "daemon/shell_service.cpp"))
(value (string
"shell_service_protocol.cpp")))))))
(attribute
(key "cflags")
(expr (value (list (value (string "-D_GNU_SOURCE"))
(value (string
"-Wno-deprecated-declarations"))))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libadbd_core"))
(value (string "libdiagnose_usb"))))))
(attribute
(key "shared_libs")
(expr (value (list (value (string "libasyncio"))
((value (string "libbase"))
(value (string "libcrypto"))
(value (string "libcrypto_utils"))
(value (string "libcutils"))
(value (string "liblog")))))))
(attribute
(key "product_variables")
(expr (value (map (attribute
(key "debuggable")
(expr (value (map (attribute
(key "required")
(expr (value (list (value
(string
"remount"))))))))))))))
(attribute
(key "target")
(expr (value (map (attribute
(key "android")
(expr (value (map (attribute
(key "srcs")
(expr (value (list (value
(string
"daemon/abb_service.cpp"))
((value
(string
"daemon/framebuffer_service.cpp"))
(value
(string
"daemon/mdns.cpp"))
(value
(string
"daemon/reboot_service.cpp"))
(value
(string
"daemon/remount_service.cpp"))
(value
(string
"daemon/restart_service.cpp"))
(value
(string
"daemon/set_verity_enable_state_service.cpp")))))))
((attribute
(key "static_libs")
(expr (value (list (value
(string
"libavb_user"))))))
(attribute
(key "shared_libs")
(expr (value (list (value
(string
"libbootloader_message"))
((value (string
"libmdnssd"))
(value (string
"libext4_utils"))
(value (string
"libfec"))
(value (string
"libfs_mgr"))
(value (string
"libselinux"))))))))))))
(attribute
(key "recovery")
(expr (value (map (attribute
(key "exclude_srcs")
(expr (value (list (value
(string
"daemon/abb_service.cpp")))))))))))))))))
(module
(rule "cc_library")
(map (attribute
(key "name")
(expr (value (string "libadbd"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adbd_defaults"))
(value (string "host_adbd_supported"))))))
(attribute
(key "recovery_available")
(expr (value (bool "true"))))
(attribute
(key "use_version_lib")
(expr (value (bool "false"))))
(attribute
(key "compile_multilib")
(expr (value (string "both"))))
(attribute
(key "whole_static_libs")
(expr (value (list (value (string "libadbd_core"))))))
(attribute
(key "shared_libs")
(expr (value (list (value (string "libadbd_services"))
((value (string "libasyncio"))
(value (string "libbase"))
(value (string "libcrypto"))
(value (string "libcrypto_utils"))
(value (string "libcutils"))
(value (string "liblog")))))))
(attribute
(key "export_include_dirs")
(expr (value (list (value (string "daemon/include")))))))))
(module
(rule "cc_binary")
(map (attribute
(key "name")
(expr (value (string "adbd"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adbd_defaults"))
(value (string "host_adbd_supported"))))))
(attribute
(key "recovery_available")
(expr (value (bool "true"))))
(attribute
(key "srcs")
(expr (value (list (value (string "daemon/main.cpp"))))))
(attribute
(key "cflags")
(expr (value (list (value (string "-D_GNU_SOURCE"))
(value (string
"-Wno-deprecated-declarations"))))))
(attribute
(key "strip")
(expr (value (map (attribute
(key "keep_symbols")
(expr (value (bool "true"))))))))
(attribute
(key "shared_libs")
(expr (value (list (value (string "libadbd"))
((value (string "libadbd_services"))
(value (string "libbase"))
(value (string "libcap"))
(value (string "libcrypto"))
(value (string "libcutils"))
(value (string "liblog"))
(value (string "libminijail"))
(value (string "libselinux"))))))))))
(module
(rule "cc_binary")
(map (attribute
(key "name")
(expr (value (string "abb"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adbd_defaults"))))))
(attribute
(key "recovery_available")
(expr (value (bool "false"))))
(attribute
(key "srcs")
(expr (value (list (value (string "daemon/abb.cpp"))))))
(attribute
(key "cflags")
(expr (value (list (value (string "-D_GNU_SOURCE"))
(value (string
"-Wno-deprecated-declarations"))))))
(attribute
(key "strip")
(expr (value (map (attribute
(key "keep_symbols")
(expr (value (bool "true"))))))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libadbd_core"))
((value (string "libadbd_services"))
(value (string "libcmd")))))))
(attribute
(key "shared_libs")
(expr (value (list (value (string "libbase"))
((value (string "libbinder"))
(value (string "liblog"))
(value (string "libutils"))
(value (string "libselinux"))))))))))
(module
(rule "cc_test")
(map (attribute
(key "name")
(expr (value (string "adbd_test"))))
((attribute
(key "defaults")
(expr (value (list (value (string "adbd_defaults"))))))
(attribute
(key "srcs")
(expr ((value (var "libadb_test_srcs")) (append "+"))
(value (list (value (string "daemon/services.cpp"))
((value (string "daemon/shell_service.cpp"))
(value (string
"daemon/shell_service_test.cpp"))
(value (string "shell_service_protocol.cpp"))
(value (string
"shell_service_protocol_test.cpp")))))))
(attribute
(key "static_libs")
(expr (value (list (value (string "libadbd"))
((value (string "libbase"))
(value (string "libbootloader_message"))
(value (string "libcutils"))
(value (string "libcrypto_utils"))
(value (string "libcrypto"))
(value (string "libdiagnose_usb"))
(value (string "liblog"))
(value (string "libusb"))
(value (string "libmdnssd"))
(value (string "libselinux")))))))
(attribute
(key "test_suites")
(expr (value (list (value (string "device-tests")))))))))
(module
(rule "python_test_host")
(map (attribute
(key "name")
(expr (value (string "adb_integration_test_adb"))))
((attribute
(key "main")
(expr (value (string "test_adb.py"))))
(attribute
(key "srcs")
(expr (value (list (value (string "test_adb.py"))))))
(attribute
(key "test_config")
(expr (value (string "adb_integration_test_adb.xml"))))
(attribute
(key "test_suites")
(expr (value (list (value (string "general-tests"))))))
(attribute
(key "version")
(expr (value (map (attribute
(key "py2")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"false"))))))))
(attribute
(key "py3")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"true")))))))))))))))
(module
(rule "python_test_host")
(map (attribute
(key "name")
(expr (value (string "adb_integration_test_device"))))
((attribute
(key "main")
(expr (value (string "test_device.py"))))
(attribute
(key "srcs")
(expr (value (list (value (string "test_device.py"))))))
(attribute
(key "libs")
(expr (value (list (value (string "adb_py"))))))
(attribute
(key "test_config")
(expr (value (string "adb_integration_test_device.xml"))))
(attribute
(key "test_suites")
(expr (value (list (value (string "general-tests"))))))
(attribute
(key "version")
(expr (value (map (attribute
(key "py2")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"true"))))))))
(attribute
(key "py3")
(expr (value (map (attribute
(key "enabled")
(expr (value (bool
"false")))))))))))))))))
- How to operate on peg:tree structure,
Malte Frank Gerdes <=