Strip comments out of a paragraph list of data

Hello.

Sometimes it is ok, to be able to have comments in datafiles, and have them sifted away, so they don’t make havoc to whatever it is that processes the data.

The handler below does that, -on a copy of the list passed to it. It is a bit quirky, since I keep the original list around, because I can read the comment from within ScriptDebugger then, should I wish to.

You can of course change the comments to your liking, But I have guessed that the block comment anchors are two, do you choose to use just one, then just repeat it. -And nested block comments aren’t allowed! :slight_smile:

to stripCommentsFromCopy(boxedListInList)
# Copyright © 2015 McUsr http://macscripter.net/viewtopic.php?id=43958
	-- It strips a the contents of a datafile for comments
	script o
		property lineComment : {"--", "#"}
		property blockComment : {"(*", "*)"}
		property l : boxedListInList
		property m : item 1 of boxedListInList
	end script
	set ll to length of o's m
	set lclen to (length of o's lineComment)
	set inBlockComment to false
	set tids to AppleScript's text item delimiters
	repeat with i from 1 to ll
		
		set AppleScript's text item delimiters to {space, tab}
		set probe to text items of item i of o's m
		set AppleScript's text item delimiters to ""
		set probe to probe as text
		
		if not inBlockComment then
			
			repeat with j from 1 to lclen
				if probe starts with item j of o's lineComment then
					set item i of o's m to missing value
					exit repeat
				end if
			end repeat
			if item i of o's m is not missing value then
				if probe starts with item 1 of o's blockComment then
					set item i of o's m to missing value
					set inBlockComment to true
				end if
			end if
		else if probe starts with item 2 of o's blockComment then
			set item i of o's m to missing value
			set inBlockComment to false
		else
			set item i of o's m to missing value
		end if
	end repeat
	set AppleScript's text item delimiters to tids
	if inBlockComment then error "stripCommentsFromCopy: malformed block comment in paragraph list"
	set item 1 of o's l to o's m's text
end stripCommentsFromCopy


# Driver: 
set fileName to "/Some/Posix_or/hfs/path"

if fileName starts with "~/" then
	set fileName to (POSIX path of (path to home folder)) & text 3 thru -1 of fileName
end if

tell application id "sevs" to set FileFound to exists fileName

if FileFound then
	try
		set dataDef to paragraphs of (read fileName as «class utf8»)
	on error
		error "An error occured while reading " & fileName
	end try
	set toClean to {dataDef}
	stripCommentsFromCopy(toClean)
	set usableData to item 1 of toClean
else
	error "The file: " & fileName & " doesn't exist!"
end if

Sample data:

Edit
I have removed a little snag, if the handler was fed an empty list.