I finally got the Yaml parser ready for release
No idea what Yaml is Learn Yaml in 5 minutes
Save Function as [url="https://github.com/HotKeyIt/Yaml/raw/master/Yaml.ahk"]Yaml.ahk[/url] - or - [url="https://github.com/HotKeyIt/Yaml/raw/ahkV2/Yaml.ahk"]Yaml.ahk (v2 version)[/url] in your lib folder.
Note! You will need latest AutoHotkey_L or AutoHotkey_H to use these functions.
I have tried to keep Yaml 1.2 format but there are some things that I have changed and some do not work:
- Tab character can be used to intend text as well as 2 spaces.
- Keys and sequence are allowed on same level.
- - Sequence is saved in Object[""]
- - That way we can have digit keys: "1": "value"
- - One drawback is that JSON does not support it, so when Yaml is dumped to JSON, key/value map becomes a sequence object
- !!Type is currently not implemented and ignored
Please report any issues or suggestions.
Enjoy

Yaml() is used to create an object.yamlObj:=Yaml(file,isfile:=1) ;isfile is set to 1 by default yamlObj:=Yaml(YamlText,0) ;so we need to use 0 to load text.- file = Can be a file path or yaml text, to load text set "isfile" to 0
After Yaml is parsed your object will contain the appropriate sequences and maps.
Sequences and documents are saved in separate object, this object is again saved in obj[""].
Tab instead of 2 spaces to indent text is supported as well.
Also maps and seqences are allowed within same objectyamlObj:=Yaml("Key: abc`n1: cde`n- Item1`n- Item2",0) MsgBox % yamlObj.Key MsgBox % yamlObj.1 ;key named 1 MsgBox % yamlObj.(1) ;sequence item (same as yamlObj[""].1) MsgBox % yamlObj.() ;count items saved in yamlObj[""] (same as yamlObj[""].MaxIndex())
Dump can be used to parse object back to Yaml text.
Yaml text can be returned in JSON syntax, threfore parameter need to be 0 or higher, see example.MsgBox % yamlObj.Dump(JSON_LEVEL) ;Yaml_Dump can be used to dump other (non Yaml) objects as well variable:="MyVariable" aKey:="I'm a key" Obj:={aKey: variable,"Now is": A_Now,"": ["a","b","c"]} MsgBox % Yaml_Dump(obj) MsgBox % Yaml_Dump(obj,0)Add can be used to append new yaml content to existing object, set second parameter to 1 to append a fileMyYaml:=Yaml("",0) ;create empty Yaml MyYaml.Add("Test: AHK") ;add new map MyYaml.Add("- Item2") ;add new sequence MyYaml.Add("- - Item3") ;add new object and insert new item MyYaml.(3).Add("- Item4") ;add an item to above created object MyYaml.Add("Key: [b]") ;you can also overwrite values MyYaml.Key.Add("- c") ;add new sequence item to Key MyYaml.Key.Add("[a,h,k]") ;add new item containing object of sequences MyYaml.Key.Add("{a:1,b:2,c:3}") ;add new item containing object of maps MsgBox % MyYaml.Dump(1)
Example and more information[/b] - Example uses ObjTree() to present yaml object
Gosub, LoadYaml1 MyYaml:=Yaml(yamlText,0) ObjTree(MyYaml,"Yaml loaded from variable") MsgBox % MyYaml.(1).Foreword.() ;count sequence items in Foreword MsgBox % MyYaml.(1).Foreword.(2) ;show content of first item in Foreword MyYaml.Add("New Key: AutoHotkey") ;add a key/value pair. MsgBox % MyYaml.Dump(3) ;dump yaml object to string ObjTree(Yaml(MyYaml.Dump(1),0),"Yaml loaded from dumped object") ExitApp LoadYaml1: yamlText= ;example yaml data. (RTrim0 `%YAML 1.2 #version statement is simply ignored - Foreword: - Tabs - Though Yaml originally does not support tabs, I decided to implement/accept them in Yaml(). So you can use "Tab" or "2 Spaces" to indent the text. If more than a Tab or 2 spaces are used, map or sequence will be shifted to the latest item. - objects can hold maps and sequences at same time - Comments - Comments are ignored currently Special Characters: Escape chars: - '"{[{]}\n' # " is allowed inside ' ', here chars can't be escaped and ' inside is not allowed - "'"\nnew line" # key or value enclosed in " use \ to escape " and new line \n (CRLF is converted to LF) - |+ # Scalars can be used for sequences as well as for maps a b c "all \nchars": {[:""' # keys and values can be enclosed in quotes --- # new document # this will insert an object in main object # all elements will belong to it until next --- or ... is encountered # --- is not different to sequence (- ) and indented context. # |--- # |- Object: |Object: # | key: value | key:=value # these documents are inserted in our main object same as lists # so yaml.1 will access a document or first sequence item in our document # if key is digit number (e.g. "1: value"), you will need to use yaml[1 ""] to access it, not yaml.1 !!! Object: # a simple Key, since there is no value it is an object Key: Value # this key is saved in above object and is assigned a value Sequence: # sequence elements can be accessed using digit keys - Element 1 # yaml.1.Object.Sequence.1 - Element 2 Map: # map elements cal be accessed as usual keys in object key: value # yaml.1.Object.Map.key another key: value # yaml.1.Object.Map["another key"] 1: value # yaml.1.Object.Map[1 ""] Inline Sequence: [a, b, c] # we can create inline sequence or map Inline Map: { key1 : value # sequences and maps can be split into several lines , key2 : value2, key3 : value3 # yaml.1.Object["Inline Map"].key2 , key4 : value4} Sequence of sequences: - [a, b, c] - [1, 2, 3, " ", " ", "2" , "1", "another value "] Map of maps: 2: {key:value, 1:test} map: {key1:value1, 2:AHK} Sequences and Maps mixed: - [a, {key:"value"}, b, [c, d, e], f, [g, h]] - {key:[a , b, c], test:{another:value}} Test: - a - b - - new object - same object as above - e - line is appended ) returnChange log
02.05.2014 - Version 1.0.0.14
- Moved downloads to github
- Fixed some errors
29.10.2012 - Version 1.0.0.13
- Fixed a bug with array key combined "- Key:"
- Removed NXT from vars to be cleared.
29.10.2012 - Version 1.0.0.12
- Fixed issue with continuing lines and some variables that were not cleared
28.10.2012 - Version 1.0.0.11
- Fixed an issue with {} and # in quotes
21.10.2012 - Version 1.0.0.10
- Fixed incorrect object creation as pointed out by Lexikos.
- Improved JSON numbers parsing. Now both chars before and after comma need to be digits to be kept together, see this post.
01.04.2012 - Version 1.0.0.9
- Again another fix for multiple indentation
31.03.2012 - Version 1.0.0.8
- Another fix for multiple indentation
30.03.2012 - Version 1.0.0.7
- Multiple indentation will be always reset to previous level + 1
30.03.2012 - Version 1.0.0.6
- Fixed a bug with static variables
20.03.2012 - Version 1.0.0.5
- Another fix to enclose #... in "".
19.03.2012 - Version 1.0.0.4
- Fixed JSON multiline statements
15.03.2012 - Version 1.0.0.2
- Fixed few cases where characters needs to be enclosed in '' or ""
14.03.2012 - Version 1.0.0.2
- Deleted duplicate declarations.
11.03.12 - Version 1.0.0.1
- Fix for empty continuation lines
03.03.2012
- Small fix if last line empty
01.03.2012
- Many fixes have been put in place to have closer support to Yaml syntax.
- Also many v2 fixes have been implemented
23.09.2011
- Added yaml.Save(file,JsonLevel) function.
13.09.2011
- Fixed unicode support (characters conversion)
17.06.2011
- Fixed to determine Unicode characters properly
24.04.2011
- Implemented Yaml Escape characters
21.04.2011
- fixed several issues