Parsing key:value note-tags

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
This is a note-tag that I enjoy using

<notetag>option1: value1option2: value2option3: value3</notetag>How would you parse this so that1. Options are optional

2. Values can span multiple lines

Examples

Code:
<notetag>name: himeclass: empressdescription: some really long descriptionthat spans multiple lines and should beparsed as one thing. New-lines should beinserted explicitly using \n in the tag</notetag>
Perhaps additional characters should be added to the note-tag to enforce structure and to be able to distinguish between keys and values? Maybe just use hash notation and eval the whole thing as a hash and let ruby deal with the parsing?
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
That is partially the motivation behind the format since it's easy to write and not verbose (don't have to worry about braces and commas and all the other syntax stuff).


If there was a YAML parser compatible with RM that might work.
 
Last edited by a moderator:

IMP1

"haystack".scan(/needle/)
Veteran
Joined
Mar 6, 2014
Messages
61
Reaction score
44
First Language
English
Primarily Uses
RMVXA
If you had a clearer beginning and ending for each option then this would be a lot easier.

Maybe something like <option: value_possible_spanning_multiple_lines>?

Or a comma/semi-colon/whatever separating the options?

Then it's a simple matter of either using a regular expression, or splitting the string at the separator.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
If you had a clearer beginning and ending for each option then this would be a lot easier.

Maybe something like <option: value_possible_spanning_multiple_lines>?

Or a comma/semi-colon/whatever separating the options?

Then it's a simple matter of either using a regular expression, or splitting the string at the separator.
So for example something like

<notetag> <name: hime> <class: empress> <description: some really long description that spans multiple lines and should be parsed as one thing. New-lines should be inserted explicitly using \n in the tag></notetag>I like it. Since they're using angle braces already it shouldn't be too much of an issue to add a few more.One of the problems I ran into was formulas.

Code:
<notetag>  <condition: a.hp > 20 && a.hp < 40 >  <class ... ></notetag>
I'm not sure how to write a regex to parse that correctly! :(
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
564
Reaction score
275
First Language
German
Primarily Uses
The easiest solution I could think of would be wrapping such sequences in special characters like quotes, braces or square brackets or explicitely escape the additional > characters.

When you only allow inherent text within your notetags, you probably could also look ahead if the start of a new tag is follwing the >, since it's not impossible, but quite uncommon to use >< in a ruby expression:

/<formula:(.*?)>(?=\s*<)/m

(Didn't test this example)

EDIT: While I rarely work with regular expressions I tried to work out a method for it, although it's far from perfect:

Code:
class String  def extract_item_note(tag, features)    # Create regular expression:    feature_patterns = features.map { |name| "(?:#{ name }\\s??<#{ name }>.*?))" }    regexp = /<#{ tag }>\s*(?:<(?:#{ feature_patterns.join("|") }|(?:.?*))>\s*)*<\/#{ tag }>/im    # Match note:    match = self.match(regexp)    return match == nil ? nil : features.map { |name| match[name] && match[name].strip }  endendnote = "This is a notebox \n" \       "<notetag>\n" \       "  <name: Heinz Reiher> \n" \       "  <condition: 25 > 36 and 48 is my favorite number >:-)> \n" \       "<name: Her Mann Kuntz> \n" \       "</notetag>"p note.extract_item_note("notetag", ["name", "class", "condition"])
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
I'm not sure how to write a regex to parse that correctly!
possibly just do another <start><end> kind of tag for formulas

so that would be

Code:
<notetag>  <condition>     a.hp > 20 && a.hp < 40  </condition>  <class ... ></notetag>
 

IMP1

"haystack".scan(/needle/)
Veteran
Joined
Mar 6, 2014
Messages
61
Reaction score
44
First Language
English
Primarily Uses
RMVXA
possibly just do another <start><end> kind of tag for formulas

so that would be

<notetag> <condition> a.hp > 20 && a.hp < 40 </condition> <class ... ></notetag>
Or just end the closing tag with a forward slash (or something else), like

Code:
<condition: a.hp > 20 && a.hp < 40 />
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,865
Messages
1,017,059
Members
137,574
Latest member
nikisknight
Top