Grammar-kit tutorial? .bnf and .flex file instrcution? - grammar-kit

I'm doing a plugin for other language, with Grammar-Kit. -> https://github.com/JetBrains/Grammar-Kit
I'm not sure what .bnf and .flex files are responsible for? From my view, .bnf defines how operations in this language looks like: eg. assign value as (a = 1) formate. What about .flex file ?

Related

Dynamic struct member names like in javascript in golang

I am writing a multi-lang website.
I read the language info from users cookies, and I have several translation modules such as en.go gr.go etc.
The modules are of type map[string]string.The problem here is in javascript I can do something like lang[cookies.lang]["whatever message"].'But go does not support accessing struct members in this way.
I could make switch case ormap[string]map[string]string` and map all possible languages, but this is much extra work.
So is there any way golang provides some way to access members like js brackets notation?
Not: There was a similar question on the stack, and somebody wrote to use "reflect" package, but I could not quite understand how it works and failed to reproduce by myself works and failed to reproduce by myself.
One possible route would be to use a map[string]map[string]string.
You could then have a base package in which you declare your base translation variable and in your translation modules, you can use an init function to populate the relevant sub-map. It's essentially optional if you want to do this as separate packages or just separate files (doing it as packages means you have better compile-time control of what languages to include, doing it as files is probably less confusing).
If you go the packages root, I suggest the following structure:
translation/base This is where you export from
translation/<language> These are "import only" packages
Then, in translation/base:
package "base"
var Lang map[string]map[string]string
And in each language-specific package:
package "<language code>"
import "language/base"
var code = "<langcode>"
func init() {
d := map[string]string{}
d[<phrase1>] = "your translation here"
d[<phrase2>] = "another translation here"
// Do this for all the translations
base.Lang[code] = d
}
Then you can use this from your main program:
package "..."
import (
"language/base"
_ "language/lang1" // We are only interested in side-effects
_ "language/lang2" // Same here...
)
Not using separate packages is (almost) the same. You simply ensure that all the files are in the same package and you can skip the package prefix for the Lang variable.
A toy example on the Go Playground, with all the "fiddly" bits inlined.

Accessing custom jbake confing properties in asciidoc

After some time I spent staring at the jbake code, I figured out that if I declare my own property in jbake.properties :
...
foo=bar
...
I can reuse that in files that go through a template engine by referencing it as ${config.foo}. I'd like to have this substitution working also on the content lvl, i.e. for files written in asciidoc, living inside the content directory.
Is there any non-trivial way to achieve it? How can I make the templating engine to proccess the result of asciidoc parses engine, or make it running it before the asciidoctor?
I found the answer myself.
To use the property substitution in asciidoc files, add following to the jbake.properties:
...
asciidoctor.attributes.export=true
foo=world
...
and reference the variable in aFile.adoc this way:
Hello {foo}!

Loop through files in a directory or a list

I am trying to generate multiple CSS classes for a single .css file to be used by the body element of a page to change the page's entire color scheme.
I have a folder of .less files containing variables #base00 to #base0F for their specific color scheme (https://github.com/AndrewBelt/hack.chat/tree/master/client/base16) and would like to import each of these files for each CSS class name.
Here's some psuedocode to achieve what I need.
// This syntax does not exist in LESS
for each #scheme in ./base16/ {
#import "#scheme"
body.#{scheme} {
background: #base00;
color: #base07;
}
...
}
I might have to think outside of the box for this one, like creating a Makefile to build by replacing variable by command line and concatenate each .css file generated by LESS to a single master .css file. But perhaps there is a more elegant way using pure LESS.
No, there's no built-in file system functions/features Less. (It's designed to work in several environments and some of those do not even permit "directory sniffing"). If necessary one can write a plugin to provide such functionality, but I suppose in this case it would be more easy to do this externally.
Compiling and concatenating multiple files is not necessary the simplest method. For example you can simply generate Less file that imports all schemes and applies each to a main template, e.g. just a list of:
.scheme-name {
#import "scheme-name";
#import (multiple) "scheme-styles-template";
}
statements. Where scheme-styles-template is the same as your scheme.less except body to be defined as:
body& {
background: #base00;
color: #base05;
}

Dynamic including/autoload of external TypoScript templates

Is it possible to include automatically all external TS templates in some folders to avoid manual linking ?
Instead of writing each time manually INCLUDE_TYPOSCRIPT like this :
Constants :
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/1.ts">
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/2.ts">
...
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/999.ts">
Setup :
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/constants/1.ts">
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/constants/2.ts">
...
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/constants/999.ts">
I would like to have something like this instead of manual include file per file :
Constants :
<INCLUDE_TYPOSCRIPT_ALL: source="FILE:fileadmin/templates/typoscript/constants/">
Setup :
<INCLUDE_TYPOSCRIPT_ALL: source="FILE:fileadmin/templates/typoscript/setup/">
Recursive option could be helpful also.
Is it possible, or I need to hack the core to realize such functionality ?
The only way is including sub-files in one main file. It's similar approach like using #import in CSS files for including other substyles.
Then you can just include one file in your TS:
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/main.ts">
and at the main.ts you can paste additional includes

How to design a class for managing file path?

In my app, I generate some xml file for instance : "/xml/product/123.xml" where 123 is the product's id and 123.xml contains informations about this product. I also have "/xml/customer/123.xml" where 123.xml contains informations about the client ... 123
How can I manage these file paths :
I create the file path directly in the serialization method ?
I create 2 static classes : CustomerSerializationPathManager and ProductSerializationPathManager with 1 method : getPath(int customerID) and getPath(int productID)
I create one static class : SerializationPathManager with 2 methods : getCustomerPath(int customerID) and getProductPath(int productID)
Something else
I'd prefer solution 3 because if I think there's only one reason to change this class : I change the root directory.
So I'd like to have your thoughts about it... thx
If you need to save the files in specific folders and the location of these files can change, then you should move this information in a configuration file and later use if from there.
You then create a class similar to a factory, with getPathForProductExports, getPathForCustomerExports etc which reads the configuration file to return the desired path.
The configuration file can be a simple .properties file:
customer_path=/xml/customer/
product_path=/xml/product/
When generating the XML (be it customer, product or whatever) you prepend the appropriate path (getPathForCustomerExports, getPathForProductExports) to the file name.
If you later change the location you just edit the config file.