aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 773c77a243b24e7753d191a9df6fb8b7bf3026fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# conf

Go библиотека для парсинга конфигурационных файлов `.conf` похожем на
классические UNIX конфиги, как у nginx или bind9.

[English version](#english)

## Установка

```bash
go get go.neonxp.ru/conf
```

## Особенности формата

- **Команды**: `directive arg1 arg2;`
- **Команды с телом**: `directive arg1 arg2 { ... }`
- **Типы аргументов**: строки (двойные/одинарные кавычки/backticks для многострочных строк), числа (целые/дробные), булевы значения
- **Вложенные блоки**: произвольная глубина вложенности
- **Комментарии**: `#` до конца строки
- **UTF-8**: включая кириллицу

## Быстрый старт

```go
package main

import (
    "fmt"
    "go.neonxp.ru/conf"
)

func main() {
    // Загрузка из файла
    cfg, err := conf.LoadFile("config.conf")
    if err != nil {
        panic(err)
    }

    // Получение команды и её значения
    if hostCmd := cfg.Get("server"); hostCmd != nil {
        fmt.Printf("Server: %v\n", hostCmd.Value())
    }

    // Навигация по вложенной структуре
    sslEnabled := cfg.Get("server").Group.Get("ssl").Group.Get("enabled")
    fmt.Printf("SSL enabled: %v\n", sslEnabled.Value())
}
```

## Пример конфигурационного файла

```conf
# Простые команды без тела
listen 8080;
host "127.0.0.1";
debug false;

# Команды с аргументами и телом
server "web" {
    host "localhost";
    port 8080;

    ssl {
        enabled true;
        cert "/etc/ssl/cert.pem";
        key "/etc/ssl/key.pem";
    }

    middleware "auth" {
        enabled true;
        secret "secret123";
    }
}

# Несколько команд с одинаковым именем
cache "redis" {
    host "redis.local";
    port 6379;
}

cache "memcached" {
    host "memcached.local";
    port 11211;
}

# Многострочные строки
template `
    <!DOCTYPE html>
    <html>
        <body>Hello</body>
    </html>
`;
```

## API

### Загрузка конфигурации

```go
// Из файла
cfg, err := conf.LoadFile("path/to/config.conf")

// Из памяти
cfg, err := conf.Load("inline", []byte("listen 8080;"))
```

### Типы данных

```go
// model.Ident — идентификатор команды (псевдоним для string)
ident := model.Ident("server")

// model.Command — команда с именем, аргументами и вложенной группой
type Command struct {
    Name  Ident  // Имя команды
    Args  []any  // Аргументы команды
    Group Group  // Вложенные команды (тело команды)
}

// model.Group — срез команд
type Group []Command
```

### Методы Command

| Метод         | Описание                                     |
| ------------- | -------------------------------------------- |
| `Value() any` | Возвращает первый аргумент команды или `nil` |

### Методы Group

| Метод                                                      | Описание                                                   |
| ---------------------------------------------------------- | ---------------------------------------------------------- |
| `Get(name Ident) *Command`                                 | Возвращает первую команду с указанным именем или `nil`     |
| `Filter(predicate func(*Command) bool) iter.Seq[*Command]` | Возвращает итератор по командам, удовлетворяющим предикату |

## Навигация по конфигурации

```go
// Простой доступ
listenCmd := cfg.Get("listen")
fmt.Println(listenCmd.Value()) // 8080

// Цепочка вложенной навигации
port := cfg.Get("server").Group.Get("http").Group.Get("port")
fmt.Println(port.Value()) // 8080

// Использование Filter
for cmd := range cfg.Filter(func(c *model.Command) bool {
    return c.Name == "cache"
}) {
    fmt.Printf("Cache: %v\n", cmd.Value())
}
```

## Работа с аргументами

Аргументы сохраняются в срез `Args` типа `[]any`. Возможные типы:

```go
// Строка в двойных кавычках: "value"
// Строка в одинарных кавычках: 'value'
// Строка в backticks: `value`
// Число целое: 42
// Число дробное: 3.14
// Булево значение: true, false

// Пример доступа к аргументам:
cmd := cfg.Get("test")
if cmd != nil && len(cmd.Args) > 0 {
    val1 := cmd.Args[0]      // Первый аргумент
    val2 := cmd.Args[1]      // Второй аргумент

    // Приведение типа для строк
    if str, ok := val1.(string); ok {
        fmt.Println("String:", str)
    }

    // Приведение типа для чисел
    if num, ok := val2.(int); ok {
        fmt.Println("Int:", num)
    }
}
```

## Грамматика (PEG)

Формат использует PEG (Parsing Expression Grammar). Грамматика описана в файле `parser/grammar.peg`.

Основные правила:

- Все команды без тела заканчиваются точкой с запятой `;`
- Тело команды заключается в фигурные скобки `{ }`
- Аргументы разделяются пробелами
- Комментарии начинаются с `#`

## Требования

- Go 1.23+ (для использования `iter.Seq`)

## Лицензия

Этот проект лицензирован в соответствии с GNU General Public License версии 3
(GPLv3). Подробности смотрите в файле [LICENSE](LICENSE).

```
                    GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2026 Alexander NeonXP Kiryukhin <i@neonxp.ru>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
```

## Автор

- Александр Кирюхин <i@neonxp.ru>

---

<a name="english"></a>

# conf (English)

Go library for parsing `.conf` configuration files (like many classic UNIX programms like nginx or bind9).

## Installation

```bash
go get go.neonxp.ru/conf
```

## Format Features

- **Commands**: `directive arg1 arg2;`
- **Commands with body**: `directive arg1 arg2 { ... }`
- **Argument types**: strings (double/single quotes, backticks for multiline strings), numbers (integer/float), boolean values
- **Nested blocks**: arbitrary nesting depth
- **Comments**: `#` until end of line
- **UTF-8**: including Cyrillic

## Quick Start

```go
package main

import (
    "fmt"
    "go.neonxp.ru/conf"
)

func main() {
    // Load from file
    cfg, err := conf.LoadFile("config.conf")
    if err != nil {
        panic(err)
    }

    // Get command and its value
    if hostCmd := cfg.Get("server"); hostCmd != nil {
        fmt.Printf("Server: %v\n", hostCmd.Value())
    }

    // Navigate through nested structure
    sslEnabled := cfg.Get("server").Group.Get("ssl").Group.Get("enabled")
    fmt.Printf("SSL enabled: %v\n", sslEnabled.Value())
}
```

## Example Configuration File

```conf
# Simple commands without body
listen 8080;
host "127.0.0.1";
debug false;

# Commands with arguments and body
server "web" {
    host "localhost";
    port 8080;

    ssl {
        enabled true;
        cert "/etc/ssl/cert.pem";
        key "/etc/ssl/key.pem";
    }

    middleware "auth" {
        enabled true;
        secret "secret123";
    }
}

# Multiple commands with same name
cache "redis" {
    host "redis.local";
    port 6379;
}

cache "memcached" {
    host "memcached.local";
    port 11211;
}

# Multiline strings
template `
    <!DOCTYPE html>
    <html>
        <body>Hello</body>
    </html>
`;
```

## API

### Loading Configuration

```go
// From file
cfg, err := conf.LoadFile("path/to/config.conf")

// From memory
cfg, err := conf.Load("inline", []byte("listen 8080;"))
```

### Data Types

```go
// model.Ident — command identifier (alias for string)
ident := model.Ident("server")

// model.Command — command with name, arguments and nested group
type Command struct {
    Name  Ident  // Command name
    Args  []any  // Command arguments
    Group Group  // Nested commands (command body)
}

// model.Group — slice of commands
type Group []Command
```

### Command Methods

| Method        | Description                                |
| ------------- | ------------------------------------------ |
| `Value() any` | Returns first argument of command or `nil` |

### Group Methods

| Method                                                     | Description                                        |
| ---------------------------------------------------------- | -------------------------------------------------- |
| `Get(name Ident) *Command`                                 | Returns first command with specified name or `nil` |
| `Filter(predicate func(*Command) bool) iter.Seq[*Command]` | Returns iterator over commands matching predicate  |

## Configuration Navigation

```go
// Simple access
listenCmd := cfg.Get("listen")
fmt.Println(listenCmd.Value()) // 8080

// Nested navigation chain
port := cfg.Get("server").Group.Get("http").Group.Get("port")
fmt.Println(port.Value()) // 8080

// Using Filter
for cmd := range cfg.Filter(func(c *model.Command) bool {
    return c.Name == "cache"
}) {
    fmt.Printf("Cache: %v\n", cmd.Value())
}
```

## Working with Arguments

Arguments are stored in `Args` slice of type `[]any`. Possible types:

```go
// Double-quoted string: "value"
// Single-quoted string: 'value'
// Backtick string: `value`
// Integer number: 42
// Float number: 3.14
// Boolean value: true, false

// Example accessing arguments:
cmd := cfg.Get("test")
if cmd != nil && len(cmd.Args) > 0 {
    val1 := cmd.Args[0]      // First argument
    val2 := cmd.Args[1]      // Second argument

    // Type assertion for strings
    if str, ok := val1.(string); ok {
        fmt.Println("String:", str)
    }

    // Type assertion for numbers
    if num, ok := val2.(int); ok {
        fmt.Println("Int:", num)
    }
}
```

## Grammar (PEG)

The format uses PEG (Parsing Expression Grammar). Grammar is described in file `parser/grammar.peg`.

Basic rules:

- All commands without body end with semicolon `;`
- Command body is enclosed in curly braces `{ }`
- Arguments are separated by spaces
- Comments start with `#`

## Requirements

- Go 1.23+ (for `iter.Seq`)

## License

This project is licensed under GNU General Public License version 3 (GPLv3).
See [LICENSE](LICENSE) file for details.

```
                   GNU GENERAL PUBLIC LICENSE
                      Version 3, 29 June 2007

Copyright (C) 2026 Alexander NeonXP Kiryukhin <i@neonxp.ru>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
```

## Author

- Alexander Kiryukhin <i@neonxp.ru>